Java Programming: Creating a Rectangle Class and Testing It - Prof. Erliang Zeng, Quizzes of Javascript programming

The java code for creating a rectangle class with instance data for length and width, a constructor, getter and setter methods, and service methods for calculating area and circumference. It also includes a driver class called rectangletest to test the rectangle class. The use of modifiers public, private, and protected, and explains the differences between constructors and regular methods.

Typology: Quizzes

Pre 2010

Uploaded on 03/11/2009

koofers-user-tf7
koofers-user-tf7 🇺🇸

9 documents

1 / 2

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Quiz 7
Name: Panther ID:
Please use 10 minutes to finish this quiz.
1. [10 pts] Design and implement a class called Rectangle that contains instance data
representing the rectangle’s length and width. Define the rectangle constructor to accept
and initialize the length and width, and include getter and setter methods for the length
and width. Include methods that calculate and return the area and circumference of the
rectangle. Include a toString method that returns a one-line description of the rectangle.
Please also write a driver class called RectangleTest to test the class you have created.
Note: area = length * width circumference = 2 * (length + width)
// File: Rectangle.java
public class Rectangle {
//Data Declaration
private double width, length;
//Constructor
public Rectangle (double w, double l){
width = w;
length = l;
}
//Accessor
public double getWidth(){
return width;
}
public double getLength(){
return length;
}
//Mutator
public void setWidth(double w){
width = w;
}
public void setLength(double l){
length = l;
}
//Service methods: calcArea and calcCircumference
public double calcArea(){
return width*length;
}
public double calcCircumference(){
return 2*(width + length);
}
//toString
public String toString(){
return(“Rectangle:\n\tWidth - ” + width + “\n\tLength - ” + length);
}
}
//File RectangeTest.java
import java.util.Scanner;
public class RectangleTest{
public static void main(String []args){
Scanner scan = new Scanner(System.in);
double width, length;
System.out.println(“Enter Rectangle width: ”);
width = scan.nextDouble();
System.out.println(“Enter Rectangle length: ”);
length = scan.nextDouble();
Rectangle rec = new Rectangle(width, length);
System.out.println(“width: ” + rec.getWidth() + “ Length: ” +
rec.getLength());
System.out.println(“Enter new Width: ”);
pf2

Partial preview of the text

Download Java Programming: Creating a Rectangle Class and Testing It - Prof. Erliang Zeng and more Quizzes Javascript programming in PDF only on Docsity!

Quiz 7

Name: Panther ID:

Please use 10 minutes to finish this quiz.

1. [10 pts] Design and implement a class called Rectangle that contains instance data

representing the rectangle’s length and width. Define the rectangle constructor to accept

and initialize the length and width, and include getter and setter methods for the length

and width. Include methods that calculate and return the area and circumference of the

rectangle. Include a toString method that returns a one-line description of the rectangle.

Please also write a driver class called RectangleTest to test the class you have created.

Note: area = length * width circumference = 2 * (length + width)

// File: Rectangle.java public class Rectangle { //Data Declaration private double width, length; //Constructor public Rectangle (double w, double l){ width = w; length = l; } //Accessor public double getWidth(){ return width; } public double getLength(){ return length; } //Mutator public void setWidth(double w){ width = w; } public void setLength(double l){ length = l; } //Service methods: calcArea and calcCircumference public double calcArea(){ return widthlength; } public double calcCircumference(){ return 2(width + length); } //toString public String toString(){ return(“Rectangle:\n\tWidth - ” + width + “\n\tLength - ” + length); } } //File RectangeTest.java import java.util.Scanner; public class RectangleTest{ public static void main(String []args){ Scanner scan = new Scanner(System.in); double width, length; System.out.println(“Enter Rectangle width: ”); width = scan.nextDouble(); System.out.println(“Enter Rectangle length: ”); length = scan.nextDouble(); Rectangle rec = new Rectangle(width, length); System.out.println(“width: ” + rec.getWidth() + “ Length: ” + rec.getLength()); System.out.println(“Enter new Width: ”);

rec.setWidth(scan.nextDouble()); System.out.println(“Enter new Length: ”); rec.setLength(scan.nextDouble()); System.out.println(rec.toString()); System.out.println(rec); //same as the previous statement } }

[Bonus]

[1 pt] Can you name modifiers you have learned?

public, private, protected

[1 pt] Why are modifiers necessary?

Modifiers are used to maintain encapsulation

[1 pt] What are differences between Constructor and regular methods defined within

class?

Constructor name is same as class name and constructor has no return type and no

void.

[2 pt] Can you name ten or more java reserved words you have learned so far?

int, long, short, byte, char, boolean, true, false, float, double, public, private, protected, class, static, return, void