You are on page 1of 4

MA. RIKA PAULA B.

SANTIAGO
BSCS – 1A
18-020007

CS113 – Problem Set 1


“CONSTRUCTORS”

EXERCISE 1

import java.util.Scanner;

public class Constructor1 {

public static class Rectangle {

int width;
int height;
int area;

void getWidth(int w) {
this.width = w;
}

void getHeight(int h) {
this.height = h;
}

void computeArea() {
this.area = this.width * this.height;
}
}

public static void main(String[] args){

Scanner scan = new Scanner(System.in);


Rectangle rectangle1 = new Rectangle();
Rectangle rectangle2 = new Rectangle();

System.out.print("Enter width for R1: ");


rectangle1.width(scan.nextInt());
System.out.print("Enter height for R1: ");
rectangle1.height(scan.nextInt());
rectangle1.computeArea();

System.out.print("Enter width for R2: ");


rectangle2.width(scan.nextInt());
System.out.print("Enter height for R2: ");
rectangle2.height(scan.nextInt());
rectangle2.computeArea();

if (rectangle1.area == rectangle2.area) {
System.out.println("The area of the rectangles are the same.");
} else {
System.out.println("The area of the rectangles are not the same.");
}
}
}
EXERCISE 2

import java.util.Scanner;

public class Constructor2


{
public static class Calendar {
int numberOfDays;
int month;
int year;

public int compute() {


int[] thirtyDayMonths = new int[]{9, 4, 6, 11};
int[] thirtyOneDayMonths = new int[]{1, 3, 5, 7, 8, 10, 12};
int i;

if (((this.year % 4) == 0) && (this.month == 2)) {


numberOfDays = 29;
} else if (this.month == 2) {
numberOfDays = 28;
} else {
for (i = 0; i <= 3; i++) {
if (this.month == thirtyDayMonths[i]) {
this.numberOfDays = 30;
}
}

for (i = 0; i <= 3; i++) {


if (this.month == thirtyOneDayMonths[i]) {
this.numberOfDays = 31;
}
}
}
return numberOfDays;
}
}

public static void main(String[] args) {


Scanner scan = new Scanner(System.in);
Calendar cal = new Calendar();

System.out.println("Calendar Assistant");

System.out.print("Enter Month: ");


cal.month = scan.nextInt();
System.out.print("Enter Year: ");
cal.year = scan.nextInt();

if ((cal.month < 1) || (cal.month > 12)) {


System.out.println("Invalid input.");
} else {
System.out.println("Days: " + cal.compute());
}
}
}
EXERCISE 3

import java.util.Scanner;
public class Constructor3
{
float us, eu, bp;
float usdollar;
float euro;
float british;
float usamount;
float euroamount;
float britishamount;

Constructor 3(){

float usdollar= 52.301186f;


float usamount=0.019120f;
float euro= 59.062356f;
float euroamount=0.016931f;
float british= 68.683595f;
float britishamount=0.014560f;

public static float solve (float a, float b, float c) {


return a*b / c

Scanner sc = new Scanner(System.in);


Constructor3 currency = new Constructor3();

System.out.println("Enter Amount:");

System.out.print("USD Account:"); currency.usd = sc.nextInt();

System.out.print("EU Account:"); currency.eu = sc.nextInt();

System.out.print("BP Account:"); currency.bp = sc.nextInt();

float Valueus=solve(currency.usdollar,currency.usd,currency.usamount);
float Valueeu=solve(currency.euro,currency.eu,currency.euroamount);
float Valuebp=solve(currency.british,currency.bp,currency.britishamount);

if (Valueus > Valueeu && Valueus > Valuebp ){System.out.println("Largest amount is


US Account: " +Valueus); }

else if (Valueeu > Valueus && Valueeu > Valuebp ){System.out.println("Largest amount
is EU Account: " +Valueeu); }

else if (Valuebp > Valueus && Valuebp > Valueeu ){System.out.println("Largest


amount is BP Account: " +Valuebp); }

}
}

You might also like