I have this program: It passes on certain test cases. The Demo five has the %.1f to cut down the number to one decimal place. But I get a failed attempt such as:
Enter length: \n
Enter width: \n
You entered: 87.3, 2.0, and 174.7\n
-- Rectangle info --\n
Length: 87.34\n
Width: 2.0\n
Area: 174.68\n
And I know it is because the length and area both have 2 decimal places. Could you help me fix this? Thank you.
Program Content:
import java.util.Scanner;
public class Rectangle {
public static double getLength(Scanner keyboard) {
System.out.println("Enter length: ");
return keyboard.nextDouble();
}
public static double getWidth(Scanner keyboard) {
System.out.println("Enter width: ");
return keyboard.nextDouble();
}
public static double getArea(double length, double width) {
return length*width;
}
public static void displayData(double length, double width) {
System.out.println("-- Rectangle info --");
System.out.println("Length: "+length);
System.out.println("Width: "+width);
System.out.println("Area: "+(length*width));
}
}
Demo 5 content:
import org.w3c.dom.css.Rect;
import java.util.Scanner;
public class Demo5
{
public static void main(String[] args)
{
Scanner keyboard = new Scanner(System.in);
double length = Rectangle.getLength(keyboard);
double width = Rectangle.getWidth(keyboard);
double area = Rectangle.getArea(length, width);
System.out.printf("You entered: %.1f, %.1f, and %.1f\n", length, width, area);
Rectangle.displayData(length, width);
}
}
Please find the fixed code below along with the output. The issue is while printing the data, it is not formatted, that's why its showing with 2 decimal places:
import java.util.Scanner;
class Rectangle {
public static double getLength(Scanner keyboard) {
System.out.println("Enter length: ");
return keyboard.nextDouble();
}
public static double getWidth(Scanner keyboard) {
System.out.println("Enter width: ");
return keyboard.nextDouble();
}
public static double getArea(double length, double width) {
return length*width;
}
public static void displayData(double length, double width) {
System.out.println("-- Rectangle info --");
System.out.printf("Length: %.1f\n", length);
System.out.printf("Width: %.1f\n", width);
System.out.printf("Area: %.1f\n", (length*width));
}
}
class Demo5
{
public static void main(String[] args)
{
Scanner keyboard = new Scanner(System.in);
double length = Rectangle.getLength(keyboard);
double width = Rectangle.getWidth(keyboard);
double area = Rectangle.getArea(length, width);
System.out.printf("You entered: %.1f, %.1f, and %.1f\n", length, width, area);
Rectangle.displayData(length, width);
}
}
------------------------------------------------
OUTPUT:

I have this program: It passes on certain test cases. The Demo five has the %.1f...
I am trying to write a Geometry.java program but Dr.Java is giving me errors and I dont know what I am doing wrong. import java.util.Scanner; /** This program demonstrates static methods */ public class Geometry { public static void main(String[] args) { int choice; // The user's choice double value = 0; // The method's return value char letter; // The user's Y or N decision double radius; // The radius of the circle double length; // The length of...
// please i cant understand why this program isnot running HELP me please? import java.util.Scanner; public class String { public static void main(String[] args) { double Sside, Rlength, Rwidth, Tbase, Theight, Area, Tarea, Rarea; String input; Scanner keyboard = new Scanner(System.in); System.out.print("To Calculate the are of Square Enter 'Square', For Rectangle Enter 'Rectangle', For Triangle Enter 'Triangle'"); input = keyboard.nextLine(); if (input.equalsIgnoreCase("SQUARE")) { System.out.println("Square Side"); Sside = keyboard.nextInt(); Tarea = Sside * Sside; System.out.println("Side = " + Tarea ); }...
******** IN JAVA ********* I have a program that I need help debugging. This program should ask a user to input 3 dimensions of a rectangular block (length, width, and height), and then perform a volume and surface area calculation and display the results of both. It should only be done using local variables via methods and should have 4 methods: getInput, volBlock, saBlock, and display (should be void). I have most of it written here: import java.util.*; public class...
JAVA Modify the code to create a class called box, wherein you find the area. I have the code in rectangle and needs to change it to box. Here is my code. Rectangle.java public class Rectangle { private int length; private int width; public void setRectangle(int length, int width) { this.length = length; this.width = width; } public int area() { return this.length * this.width; } } // CONSOLE / MAIN import java.awt.Rectangle; import java.util.Scanner; public class Console...
Why is my program returning 0 for the area of a triangle? public class GeometricObjects { private String color = " white "; private boolean filled; private java.util.Date dateCreated; public GeometricObjects() { dateCreated = new java.util.Date(); } public GeometricObjects(String color, boolean filled) { dateCreated = new java.util.Date(); this.color = color; this.filled = filled; } public String getColor() { return color; } public void setColor(String color)...
JAVA Code Requried Copy the file java included below. This program will compile, but, when you run it, it doesn’t appear to do anything except wait. That is because it is waiting for user input, but the user doesn’t have the menu to choose from yet. We will need to create this. Below the main method, but in the Geometry class, create a static method called printMenu that has no parameter list and does not return a value. It will...
Please, modify the code below to use the Switch Statements in Java instead of “if” statements to make the decisions. import java.util.Scanner; public class Area { public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.print("Enter code(C for circle, R for rectangle, S for square): "); char code = in.next().charAt(0); if(code == 'C') { System.out.print("Enter radius: "); double radius = in.nextDouble(); System.out.println("Area of circle is " + (Math.PI * radius * radius)); } else if(code == 'R') {...
9780134457963
i try to run the demo and it says resource leak keyboard is
never closed line 59 and 65. not sure how to fix this.
java - Eclipse IDE Et Run Window Help { 1 Payroll java PayrollDemojava 21 8-import java.util.Scanner; 9 import java.text.DecimalFormat; 10 11 12 13 public class Payroll Demo 14 15 16 17 public static void main(Strinel] args) 18 19 Payroll syPayroll new Payroll(); 20 setup Employees Payroll) 21 displayGross(ayPayroll); 22 23 24- public static void...
Why are obj1 and obj2 printing the same areas? I'm trying to learn about Comparable interface. Couldn't figure it out. the compare to method should print 0, 1, or -1 import java.util.*; public class RectangleMain { public static void main(String [] args) throws CloneNotSupportedException { /*ComparableRectangleAlsoCloneable obj1 = new ComparableRectangleAlsoCloneable(4, 5); ComparableRectangleAlsoCloneable obj2 = (ComparableRectangleAlsoCloneable)obj1.clone(); System.out.println(obj1.toString()); System.out.println(obj1 == obj2); //false System.out.println(obj2.toString());*/ Scanner...
make this program run import java.util.Scanner; public class PetDemo { public static void main (String [] args) { Pet yourPet = new Pet ("Jane Doe"); System.out.println ("My records on your pet are inaccurate."); System.out.println ("Here is what they currently say:"); yourPet.writeOutput (); Scanner keyboard = new Scanner (System.in); System.out.println ("Please enter the correct pet name:"); String correctName = keyboard.nextLine (); yourPet.setName (correctName); System.out.println ("Please enter the correct pet age:"); int correctAge = keyboard.nextInt (); yourPet.setAge (correctAge); System.out.println ("Please enter the...