I need help studying and these are some of the JAVA questions that I am stuck on
ID: 3723193 • Letter: I
Question
I need help studying and these are some of the JAVA questions that I am stuck on:
1. Write a class called rectangle that has a field called width, height, and area.
1a. Create constructors (create a default constructor and different argument constructors), getter and setter methods, increase width by 5 methods, and increase height by 5 method.
1b. Create a testRectangle class that tests each constructor and method.
2. Print all prime numbers from 1-100
3. Write a program that prints out a breakdown of an integer into a sum of numbers that have just one non-zero digit. For example, given 43018 it should print 40000 + 3000 + 10 + 8.
4. Write a class called Coffee that has fields called type_of_coffee, size, price.
4a. Create constructors (create a default constructor and different argument constructors), getter and setter methods, increase price method, and decrease price method.
4b. Create a tester class to test each constructor and method.
5. Print the numbers from 1 to 100. For every number that is divisible by 4 and 5, print “divisible”
6. Print every even number from 1- 100. When the number is also divisible by 3, print “java”.
Explanation / Answer
Note: As per the guide line limit of sub question to be answerd is 4.
1. Answer:
Program:
public class Rectangle {
private int width;
private int height;
private int area;
public Rectangle() {
super();
this.width = 0;
this.height = 0;
this.area = 0;
}
public Rectangle(int width, int height) {
super();
this.width = width;
this.height = height;
this.area = width*height;
}
public int getWidth() {
return width;
}
public int getHeight() {
return height;
}
public int getArea() {
area = width*height;
return area;
}
public void setWidth(int width) {
this.width = width;
}
public void setHeight(int height) {
this.height = height;
}
public void increaseWidth() {
this.width = this.width + 5;
}
public void increaseHeight() {
this.height = this.height + 5;
}
}
public class TestRectangle {
public static void main(String[] args) {
Rectangle rectangle1 = new Rectangle();
System.out.println("Height: "+rectangle1.getHeight());
System.out.println("Width: "+rectangle1.getWidth());
System.out.println("Area: "+rectangle1.getArea());
System.out.println();
rectangle1.increaseHeight();
rectangle1.increaseWidth();
System.out.println("Height: "+rectangle1.getHeight());
System.out.println("Width: "+rectangle1.getWidth());
System.out.println("Area: "+rectangle1.getArea());
System.out.println();
Rectangle rectangle2 = new Rectangle(10,20);
System.out.println("Height: "+rectangle2.getHeight());
System.out.println("Width: "+rectangle2.getWidth());
System.out.println("Area: "+rectangle2.getArea());
}
}
Output:
Height: 0
Width: 0
Area: 0
Height: 5
Width: 5
Area: 25
Height: 20
Width: 10
Area: 200
2. Answer:
Program:
public class PrimeNumbers {
public static void main(String[] args) {
int i, k, j;
for (i = 1; i <= 100; i++) {
k = 0;
for (j = 2; j < i; j++) {
if (i % j == 0) {
k = 1;
break;
}
}
if (k == 0) {
System.out.print(i + " ");
}
}
}
}
Output:
1 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97
4. Answer:
Program:
public class Coffee {
private String type_of_coffee;
private int size;
private double price;
public Coffee() {
this.type_of_coffee = "";
this.size = 0;
this.price = 0;
}
public Coffee(String type_of_coffee, int size, double price) {
this.type_of_coffee = type_of_coffee;
this.size = size;
this.price = price;
}
public String getType_of_coffee() {
return type_of_coffee;
}
public int getSize() {
return size;
}
public double getPrice() {
return price;
}
public void setType_of_coffee(String type_of_coffee) {
this.type_of_coffee = type_of_coffee;
}
public void setSize(int size) {
this.size = size;
}
public void setPrice(double price) {
this.price = price;
}
public void increasePrice(double price) {
this.price = this.price + price;
}
public void decreasePrice(double price) {
this.price = this.price - price;
}
}
public class TesterCoffee {
public static void main(String[] args) {
Coffee coffee1 = new Coffee();
coffee1.setType_of_coffee("Strong");
coffee1.setSize(10);
coffee1.setPrice(100);
System.out.println("Type: "+coffee1.getType_of_coffee());
System.out.println("Size: "+coffee1.getSize());
System.out.println("Price: $"+coffee1.getPrice());
System.out.println();
coffee1.increasePrice(20);
System.out.println("Price: $"+coffee1.getPrice());
coffee1.decreasePrice(50);
System.out.println("Price: $"+coffee1.getPrice());
System.out.println();
Coffee coffee2 = new Coffee("Mild",20,150);
System.out.println("Type: "+coffee2.getType_of_coffee());
System.out.println("Size: "+coffee2.getSize());
System.out.println("Price: $"+coffee2.getPrice());
}
}
Output:
Type: Strong
Size: 10
Price: $100.0
Price: $120.0
Price: $70.0
Type: Mild
Size: 20
Price: $150.0
5. Answer:
Program:
public class Test {
public static void main(String[] args) {
for (int i=1;i<=100;i++) {
System.out.print(i);
if (i%4 == 0 && i%5 == 0) {
System.out.print(" divisible");
}
System.out.println();
}
}
Output:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 divisible
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40 divisible
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60 divisible
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80 divisible
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100 divisible
6. Answer:
Program:
public class Test {
public static void main(String[] args) {
for (int i=1;i<=100;i++) {
if (i%2 == 0) {
System.out.print(i);
if (i%3 == 0) {
System.out.print(" java");
}
System.out.println();
}
}
}
}
Output:
2
4
6 java
8
10
12 java
14
16
18 java
20
22
24 java
26
28
30 java
32
34
36 java
38
40
42 java
44
46
48 java
50
52
54 java
56
58
60 java
62
64
66 java
68
70
72 java
74
76
78 java
80
82
84 java
86
88
90 java
92
94
96 java
98
100
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.