Question
I can't seem to out put a number entered by the user or even the number 1. import java.util.Scanner; //Write a Driver class in the
I can't seem to out put a number entered by the user or even the number 1.
import java.util.Scanner; //Write a Driver class in the same file to test your Rectangle class. It should prompt the user to enter a length and width of a rectangle, and then print out the area and perimeter of the rectangle. (Use the mutators to set the length and width of the rectangle, not the constructor.) public class Driver { public static void main(String[] args){ Rectangle sides = new Rectangle(); Scanner input = new Scanner(System.in); System.out.print("Enter length of rectangle:"); double numL = input.nextDouble(); System.out.print("Enter width of rectangle:"); double numW = input.nextDouble(); sides.setLength(numL); sides.setWidth(numW); double area = sides.area();
System.out.printf("Area: ", area); System.out.printf("Perimeter: ", sides.getLength()); } }
// Create a class Rectangle with double attributes length and width. class Rectangle { private double length; private double width; //The default constructor should set these attributes to 1. public Rectangle(){ length = 1; width = 1; } // The mutator methods for length and width should verify that the number being passed in is larger than 0.0 and less than 20.0 public void setLength(double sideL){ if(sideL > 0 && sideL < 20) { length = sideL; } } public void setWidth(double sideW){ if(width > 0 && width < 20) { this.width = width; } } // Provide methods that calculate the rectangle's perimeter and area, as well as accessors and mutators for both data fields. a = wl p =2(wl) public double getLength(){ return length; } public double getWidth(){ return width; } public double area() { return length * width; } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started