Hello the below code works, but I\'m having issues with the format specifier. It
ID: 3629866 • Letter: H
Question
Hello the below code works, but I'm having issues with the format specifier. It's not converting my System.out.printf to 2 decimal places. Can someone help with the formatting for the code of line marked 36 and 38.Thank you!
public class Rectangle
{
private double width;
private double height;
public Rectangle()
{
width = 1.0;
height = 1.0;
}
public Rectangle(double h, double w)
{
width = w;
height = h;
}
public double getArea()
{
return width * height;
}
public double getPerimeter()
{
return 2*(width + height);
}
public static void main(String[] args)
{
Rectangle R1 = new Rectangle (40,4);
Rectangle R2 = new Rectangle (35.9, 3.5);
(36.) System.out.printf(" Area is %2f", + R1.getArea() + " Perimeter is %2f", +
+ R1.getPerimeter());
(38.) System.out.printf(" Area is %2f", + R2.getArea() + " Perimeter is %2f", +
+ R2.getPerimeter());
}
}
Explanation / Answer
public class Rectangle
{
private double width;
private double height;
public Rectangle()
{
width = 1.0;
height = 1.0;
}
public Rectangle(double h, double w)
{
width = w;
height = h;
}
public double getArea()
{
return width * height;
}
public double getPerimeter()
{
return 2*(width + height);
}
public static void main(String[] args)
{
Rectangle R1 = new Rectangle (40,4);
Rectangle R2 = new Rectangle (35.9, 3.5);
System.out.printf(" Area is %.2f and Perimeter is %.2f",R1.getArea(),R1.getPerimeter());
System.out.println();
System.out.printf(" Area is %.2f and Perimeter is %.2f",R2.getArea(),R2.getPerimeter());
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.