Java 學習記錄 28 — 代碼挑戰 8

張小雄
8 min readJan 25, 2021

--

題目:

Wall Area

Write a class with the name Wall. The class needs two fields (instance variables) with name width and height of type double.

The class needs to have two constructors. The first constructor does not have any parameters (no-arg constructor). The second constructor has parameters width and height of type double and it needs to initialize the fields. In case the width is less than 0 it needs to set the width field value to 0, in case the height parameter is less than 0 it needs to set the height field value to 0.

Write the following methods (instance methods):

  • Method named getWidth without any parameters, it needs to return the value of width field.
  • Method named getHeight without any parameters, it needs to return the value of height field.
  • Method named setWidth with one parameter of type double, it needs to set the value of the width field. If the parameter is less than 0 it needs to set the width field value to 0.
  • Method named setHeight with one parameter of type double, it needs to set the value of the height field. If the parameter is less than 0 it needs to set the height field value to 0.
  • Method named getArea without any parameters, it needs to return the area of the wall.

TEST EXAMPLE

1 Wall wall = new Wall(5,4);2 System.out.println("area= " + wall.getArea());34 wall.setHeight(-1.5);5 System.out.println("width= " + wall.getWidth());6 System.out.println("height= " + wall.getHeight());7 System.out.println("area= " + wall.getArea());

→ OUTPUT:

area= 20.0
width= 5.0
height= 0.0
area= 0.0

NOTE: All methods should be defined as public NOT public static.

NOTE: In total, you have to write 5 methods and 2 constructors.

NOTE: Do not add a main method to the solution code.

Point

You have to represent a point in 2D space. Write a class with the name Point. The class needs two fields (instance variables) with name x and y of type int.

The class needs to have two constructors. The first constructor does not have any parameters (no-arg constructor). The second constructor has parameters x and y of type int and it needs to initialize the fields.

Write the following methods (instance methods):

  • Method named getX without any parameters, it needs to return the value of x field.
  • Method named getY without any parameters, it needs to return the value of y field.
  • Method named setX with one parameter of type int, it needs to set the value of the x field.
  • Method named setY with one parameter of type int, it needs to set the value of the y field.
  • Method named distance without any parameters, it needs to return the distance between this Point and Point 0,0 as double.
  • Method named distance with two parameters x, y both of type int, it needs to return the distance between this Point and Point x,y as double.
  • Method named distance with parameter point of type Point, it needs to return the distance between this Point and another Point as double.

How to find the distance between two points?To find a distance between points A(xA,yA) and B(xB,yB), we use the formula:

d(A,B)=√ (xB − xA) * (xB — xA) + (yB − yA) * (yB — yA)

Where √ represents square root.

→ TEST CODE:

Point first = new Point(6, 5);Point second = new Point(3, 1);System.out.println("distance(0,0)= " + first.distance());System.out.println("distance(second)= " + first.distance(second));System.out.println("distance(2,2)= " + first.distance(2, 2));Point point = new Point();System.out.println("distance()= " + point.distance());

OUTPUT

distance(0,0)= 7.810249675906654
distance(second)= 5.0
distance(2,2)= 5.0
distance()= 0.0

NOTE: Use Math.sqrt to calculate the square root √.

NOTE: Try to avoid duplicated code.

NOTE: All methods should be defined as public NOT public static.

NOTE: In total, you have to write 7 methods.

NOTE: Do not add a main method to the solution code.

Carpet Cost Calculator

The Carpet Company has asked you to write an application that calculates the price of carpeting for rectangular rooms. To calculate the price, you multiply the area of the floor (width times length) by the price per square meter of carpet. For example, the area of the floor that is 12 meters long and 10 meters wide is 120 square meters. To cover the floor with a carpet that costs $8 per square meter would cost $960.

1. Write a class with the name Floor. The class needs two fields (instance variables) with name width and length of type double.

The class needs to have one constructor with parameters width and length of type double and it needs to initialize the fields.

In case the width parameter is less than 0 it needs to set the width field value to 0, in case the length parameter is less than 0 it needs to set the length field value to 0.

Write the following methods (instance methods):

  • Method named getArea without any parameters, it needs to return the calculated area (width * length).

2. Write a class with the name Carpet. The class needs one field (instance variable) with name cost of type double.

The class needs to have one constructor with parameter cost of type double and it needs to initialize the field.

In case the cost parameter is less than 0 it needs to set the cost field value to 0.

Write the following methods (instance methods):

  • Method named getCost without any parameters, it needs to return the value of cost field

3. Write a class with the name Calculator. The class needs two fields (instance variables) with name floor of type Floor and carpet of type Carpet.

The class needs to have one constructor with parameters floor of type Floor and carpet of type Carpet and it needs to initialize the fields.

Write the following methods (instance methods):

  • Method named getTotalCost without any parameters, it needs to return the calculated total cost to cover the floor with a carpet.

→ TEST CODE:

Carpet carpet = new Carpet(3.5);Floor floor = new Floor(2.75, 4.0);Calculator calculator = new Calculator(floor, carpet);System.out.println("total= " + calculator.getTotalCost());carpet = new Carpet(1.5);floor = new Floor(5.4, 4.5);calculator = new Calculator(floor, carpet);System.out.println("total= " + calculator.getTotalCost());

→ OUTPUT

total= 38.5
total= 36.45

NOTE: All methods should be defined as public NOT public static.

NOTE: In total, you have to write 3 classes.

NOTE: Do not add a main method to the solution code.

Complex Operations

A complex number is a number that can be expressed in the form a + bi, where a and b are real numbers, and i is a solution of the equation x2 = −1.

Because no real number satisfies this equation, i is called an imaginary number.

For the complex number a + bi, a is called the real part, and b is called the imaginary part.

To add or subtract two complex numbers, just add or subtract the corresponding real and imaginary parts.

For instance, the sum of 5 + 3i and 4 + 2i is 9 + 5i. For another, the sum of 3 + i and –1 + 2i is 2 + 3i.

Write a class with the name ComplexNumber. The class needs two fields (instance variables) with name real and imaginary of type double.

It represents the Complex Number.

The class needs to have one constructor. The constructor has parameters real and imaginary of type double and it needs to initialize the fields.

Write the following methods (instance methods):

  • Method named getReal without any parameters, it needs to return the value of real field.
  • Method named getImaginary without any parameters, it needs to return the value of imaginary field.
  • Method named add with two parameters real and imaginary of type double, it needs to add parameters to fields. In other words,it needs to do a complex number add operation as described above.
  • Method named add with one parameter of type ComplexNumber. It needs to add the ComplexNumber parameter to the corresponding instance variables.
  • Method named subtract with two parameters real and imaginary of type double, it needs to subtract parameters from fields, in other words,it needs to do a complex number subtract operation as described above.
  • Method named subtract with one parameter other of type ComplexNumber. It needs to subtract the other parameter from this complex number.

→ TEST CODE:

ComplexNumber one = new ComplexNumber(1.0, 1.0);ComplexNumber number = new ComplexNumber(2.5, -1.5);one.add(1,1);System.out.println("one.real= " + one.getReal());System.out.println("one.imaginary= " + one.getImaginary());one.subtract(number);System.out.println("one.real= " + one.getReal());System.out.println("one.imaginary= " + one.getImaginary());number.subtract(one);System.out.println("number.real= " + number.getReal());System.out.println("number.imaginary= " + number.getImaginary());

→ OUTPUT

one.real= 2.0
one.imaginary= 2.0
one.real= -0.5
one.imaginary= 3.5
number.real= 3.0
number.imaginary= -5.0

NOTE: Try to avoid duplicated code.

NOTE: All methods should be defined as public NOT public static.

NOTE: In total, you have to write 6 methods.

NOTE: Do not add a main method to the solution code.

參考答案:

Wall Area

public class Wall {
private double width;
private double height;
public Wall() {
this(0, 0);
}
public Wall(double width, double height) { this.setWidth(width);
this.setHeight(height);
} public double getWidth() {
return width;
}
public void setWidth(double width) {
this.width = width < 0 ? 0 : width;
}
public double getHeight() {
return height;
}
public void setHeight(double height) {
this.height = height < 0 ? 0 : height;
}
public double getArea(){
return this.width * this.height;
}
}

推薦使用左上code -> generate 快捷:alt + insert

來創constructor跟set跟get

啪啪兩三下就創完了

Point

public class Point {    private int x;
private int y;
public Point() {
this(0, 0);
}
public Point(int x, int y) {
this.x = x;
this.y = y;
}
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
public double distance() {
return Math.sqrt(this.x * this.x + this.y * this.y);
}
public double distance(int x, int y) {
return Math.sqrt((this.x - x) * (this.x - x) + (this.y - y) * ((this.y - y)));
}
public double distance(Point xy) {
return Math.sqrt((this.x - xy.x) * (this.x - xy.x) + (this.y - xy.y) * (this.y - xy.y));
}
}

Carpet Cost Calculator

Floor

public class Floor {    private double width;
private double length;
public Floor(double width, double height) {
this.width = width < 0 ? 0 : width;
this.length = height < 0 ? 0 : height;
}
public double getArea(){
return this.width * this.length;
}
}

Carpet

public class Carpet {    private double cost;    public Carpet(double cost) {
this.cost = cost < 0 ? 0 : cost;
}
public double getCost() {
return cost;
}
}

Calculator

public class Calculator {    private Floor floor;
private Carpet carpet;
public Calculator(Floor floor, Carpet carpet) {
this.floor = floor;
this.carpet = carpet;
}
public double getTotalCost(){
return floor.getArea() * carpet.getCost();
}
}

Complex Operations

public class ComplexNumber {    private double real;
private double imaginary;
public ComplexNumber(double real, double imaginary) {
this.real = real;
this.imaginary = imaginary;
}
public double getReal() {
return real;
}
public double getImaginary() {
return imaginary;
}
public void add(double real, double imaginary) {
this.real += real;
this.imaginary += imaginary;
}
public void add(ComplexNumber name) {
this.add(name.real, name.imaginary);
}
public void subtract(double real, double imaginary) {
this.real -= real;
this.imaginary -= imaginary;
}
public void subtract(ComplexNumber name) {
this.subtract(name.real, name.imaginary);
}
}

--

--