43% Wed Apr 19 1:22:45 AM Q O E Preview File Edit View Go Tools Window Help Intr
ID: 3820880 • Letter: 4
Question
43% Wed Apr 19 1:22:45 AM Q O E Preview File Edit View Go Tools Window Help Introduction to java programming by Y Daniel Liang 10th edition.pdf (page 383 of 1,345) a Search v Introduction to java.programmin... Pedagogical Note three objectives The exercises in Chapters 9-13 help you achieve three objectives Design classes and draw uML class diagrams. Implement classes from the uML use classes to develop applications. Students can download solutions for the uML ams for the even-numbered exer- cises from the Companion Website, and instructors can download all solutions from the same site. Sections 9.2-9.5 9. l The Rectangle class) Following the example of the Circle class in Section 9.2, design a class named Rectangle to represent a rectangle. The class contains Two double data fields named width and height that specify the width and height of the rectangle. The default values are l for both width and height. 360 A no-arg constructor that creates a default rectangle. A constructor that creates a rectangle with the specified width and height. A method named getAreaO that returns the area of this rectan A method named get that returns the perimeter. Draw the UML diagram for the class and then implement the class, Write a test program that creates two Rectangle objects e with width 4 and height 40 and the other with width 3.5 and height 35.9. Display the width, height, area, and perimeter of each rectangle in this order. 9.2 The Stock class) Following the examp e of the Circle class in Section 9.2, design a class named Stock that contains A string data field named symbol for the stock's symbol. A string data field named name for the stock's name. A double data field named previousClosingPrice that stores the stock price for the previous day. A double data field named currentPrice that stores the stock price for the current time. A constructor that creates a stock with the specified symbol and name. A method named getChangePercent that returns the percentage changed from previousClosingPrice to currentPrice 361 Draw the UML diagram for the class and then implement the class. Write a test program that creates a Stock object with the stock symbol ORCL the nameExplanation / Answer
UML Diagram for Rectangle class
Rectangle
Width: double
height: double
Rectangle()
Rectangle(width: double, height: double)
getArea()
getPerimeter()
Rectangle.java
public class Rectangle {
double width,height;
//Constructor with default values
Rectangle(){
width=1;
height=1;
}
//Constructor with specified values
Rectangle(double width,double height){
this.width=width;
this.height=height;
}
//Method to get area of rectangle
public double getArea()
{
return width*height;
}
//Method to get perimiter of rectangle
public double getPerimeter()
{
return 2*(width+height);
}
}
TestRectangle.java
public class TestRectangle {
public static void main(String[] args)
{
Rectangle rectangle1 = new Rectangle(4,40); //Creating object of first rectangle
Rectangle rectangle2 = new Rectangle(3.5,35.9); //Creating object of second rectangle
//Printing properties of first rectangle
System.out.println("Width of first rectangle: "+rectangle1.width);
System.out.println("Height of first rectangle: "+rectangle1.height);
System.out.println("Area of first rectangle: "+rectangle1.getArea());
System.out.println("Perimeter of first rectangle: "+rectangle1.getPerimeter());
System.out.println();
//Printing properties of second rectangle
System.out.println("Width of second rectangle: "+rectangle2.width);
System.out.println("Height of second rectangle: "+rectangle2.height);
System.out.println("Area of second rectangle: "+rectangle2.getArea());
System.out.println("Perimeter of second rectangle: "+rectangle2.getPerimeter());
}
}
Output
Width of first rectangle: 4.0
Height of first rectangle: 40.0
Area of first rectangle: 160.0
Perimeter of first rectangle: 88.0
Width of second rectangle: 3.5
Height of second rectangle: 35.9
Area of second rectangle: 125.64999999999999
Perimeter of second rectangle: 78.8
Rectangle
Width: double
height: double
Rectangle()
Rectangle(width: double, height: double)
getArea()
getPerimeter()
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.