

Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
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
1 / 2
This page cannot be seen from the preview
Don't miss anything!


// 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 } }
int, long, short, byte, char, boolean, true, false, float, double, public, private, protected, class, static, return, void