Rectangle Area and Perimeter - java

I have an assignment that I am having trouble figuring out.
Write an application (Rectangle.java) that asks the user to enter the length and the width of a rectangle (both are positive double-precision floating-point numbers), and prints the area and perimeter of the rectangle. When the user enters 7.9 and 4.5, the output of your program should look exactly like the following:
Enter the length and the width of a rectangle: 7.9 4.5
The area of the rectangle is 35.55.
The perimeter of the rectangle is 24.80.
The part I am having trouble with is bringing the output of the perimeter of the rectangle out to two decimal places including the "0." I have been trying to figure it out for so long. I know there must be a simple efficient way to do this or it would not be assigned to us as our second Java homework assignment. If it is to format it as %2d i do not know how to apply that to what i have. I REALLY appreciate your help!
Here is what i have so far:
import java.util.Scanner;
public class Rectangle {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter the length and the width of a rectangle: ");
double length = input.nextDouble();
double width = input.nextDouble();
double area = (length * width);
double perimeter = (length * 2 + width * 2);
System.out.println("The area of the rectangle is " + (int)(area * 100) / 100.0 + ".");
System.out.println("The perimeter of the rectangle is " + (int)(perimeter * 100) / 100.0 + ".");
}
}
My Output:
Enter the length and the width of a rectangle: 7.9 4.5
The area of the rectangle is 35.55.
The perimeter of the rectangle is 24.8.

You need %.2f in format:
System.out.printf("The perimeter of the rectangle is %.2f", 24.8);
The 24.8 is just for test, you may replace it with the correct expresion.

Read the documentation about String.format() and the format string syntax. This will help you find the right format string to output the numbers as required.
Basically you'd have to have the last two lines of code to:
System.out.println("The area of the rectangle is " + String.format("%.2f", area) + ".");
System.out.println("The perimeter of the rectangle is " + String.format("%.2f", perimeter) + ".");

Related

How do I change the type of number? Double to int

When I run this program, I need the part where it says example " cans needed 3.0" I need it to only say the integer.
public class PaintEstimator {
public static void main(String[] args) {
// Create the Scanner object scnr
Scanner scnr = new Scanner(System.in);
// Declare the identifiers
double wallHeight;
double wallWidth;
double wallAera;
double gallonPaint = 350;
int paintNeeded;
// Prompt user for and input wall's height and width; and then display them
System.out.println("Enter wall height (feet) :");
wallHeight = scnr.nextInt();
System.out.println("Enter wall width (feet) :");
wallWidth = scnr.nextInt();
System.out.println("Wall height is: " + wallHeight + " and Wall width is: " + wallWidth);
// Calculate and output wall area
wallAera = wallHeight * wallWidth;
System.out.println("Wall area: " + wallAera + "square feet");
// Calculate and output the amount of paint in gallons needed to paint the wall
System.out.println("Paint needed: " + (wallAera / gallonPaint) + " gallons");
// Calculate and output the number of 1 gallon cans needed to paint the wall, rounded up to nearest integer
System.out.println("Cans needed: " + Math.ceil(wallAera / gallonPaint) + " can ( s )");
// Calculate and output the cost of painting the wall.
double costPaint = Math.ceil(wallAera / gallonPaint) * 45.0;
System.out.println("Cost to paint the wall: $" + costPaint);
}
}
You can round/ceil it up, as you are already doing, and then cast it to an int:
System.out.println("Cans needed: " + (int) Math.ceil(wallAera / gallonPaint) + " can ( s )");
There are 3 basic methods for change a double to an integer.
Where var-name is an integer and var-name1 is a double.
Method 1: Type-casting
Example:
int var-name = (int) var-name1;
Method 2: Rounding
Example:
int var-name = Math.round(var-name1);
Method 3: Double.intValue
Example:
int var-name = Double.intValue(var-name1);

Wall Painting Calculator. (two solutions - giving two different results)

A while ago I started to learn JAVA and everything was going well, until I got to this exercise:
CONDITIONS
Write a program that when complete will calculate the amount of paint needed to paint the walls and the ceiling of a room.
Your program should ask the length, width, and height of the room. Assume that the room has doors and windows the don't need painting. Also, the floor in the room is not painted.
Ask the user to enter the number of doors and number of windows in the room, and adjust the total square feet to be painted accordingly.
Assume that each door is 20 square feet and each window is 15 square feet.
Suppose the paint covers 350 square feet per gallon. enter code here
My chosen parameters
Width 3;
Length 10;
Height 2.0;
Doors 1;
Windows 2;
MY SOLUTION
int width = Integer.parseInt(JOptionPane.showInputDialog("Please, enter the width of a room"));
int length = Integer.parseInt(JOptionPane.showInputDialog("Please, enter the length of the room"));
double height = Double.parseDouble(JOptionPane.showInputDialog("Please, enter the height of the room"));
int doors = Integer.parseInt(JOptionPane.showInputDialog("If there are doors, please, enter the amount of them:"));
int windows = Integer.parseInt(JOptionPane.showInputDialog("If there are some windows, please, enter the amount of them:"));
double feet = 1.00/350.00;
double area = (width*length*height)-(windows*15+doors*20);
JOptionPane.showMessageDialog(null, "You need:"+feet*area+" gallons of paint.");
}
}
The output is 0.02857142
OTHER SOLUTIONby the author of the exercise
public static void main(String[] args) {
{
int length, width, numberOfDoors, numberOfWindows;
double height;
Scanner console = new Scanner(System.in);
System.out.print("Enter length: ");
length = console.nextInt();
System.out.print("Enter width: ");
width = console.nextInt();
System.out.print("Enter height: ");
height = console.nextDouble();
System.out.print("Enter number of doors: ");
numberOfDoors = console.nextInt();
System.out.print("Enter number of windows: ");
numberOfWindows = console.nextInt();
double totalSurfaceArea = 2 * (length * width + length
* height + width * height);
int areaOfFloor = length * width;
int areaOfDoors = 20 * numberOfDoors;
int areaOfWindows = 15 * numberOfWindows;
double totalPaintArea = totalSurfaceArea - areaOfFloor
- areaOfDoors - areaOfWindows;
double requiredPaint = totalPaintArea / 350;
System.out.println("Paint required "
+ requiredPaint + " gallons.");
}
}
}
The output is 0.09142857142857143;
So, two different solutions gave two different outputs.
Is there anyone who could help me to clarify, where I went wrong?

How to make a polygon attach to a point at a 3 o'clock position

I'm working on a project and I have most of it done but I'm having trouble seeing how to get the coordinates to line up. I'm stuck and I'm not sure how to get a point to be at 3 o'clock and I'm stuck. I've tried finding examples but all I see is polygons that don't need to lineup with anything. Any help?
The instructions: Suppose an n-sided regular polygon is centered at (0, 0) with one point at the 3 o’clock position, as shown in Figure 5.4. Write a program that prompts the user to enter the number of the sides, the radius of the bounding circle of a polygon, and displays the coordinates of the corner points on the polygon.
import java.util.Scanner;
public class Polygon {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter the number of sides: ");
int sides = input.nextInt();
System.out.print("Enter the radius of the bounding circle: ");
double radius = input.nextDouble();
input.close();
System.out.println("The coordinates of the points on the polygon are");
for (int i = 0; i < sides; i++) {
double x = radius * java.lang.Math.sin(2.0 * java.lang.Math.PI / sides * i);
double y = radius * java.lang.Math.cos(2.0 * java.lang.Math.PI / sides * i);
System.out.print("(");
System.out.printf("%.2f", x);
System.out.print(" ");
System.out.printf("%.2f",y);
System.out.print(")");
System.out.println();
}
}
}
You need to switch your sin and cos expressions. The the first point of your polygon will then always lie at (radius, 0), i.e. aligned with the 3-o'clock position.
double x = radius * java.lang.Math.cos(2.0 * java.lang.Math.PI / sides * i);
double y = radius * java.lang.Math.sin(2.0 * java.lang.Math.PI / sides * i);

Shape Calculator Pentagon and Hexagon Perimeter and Area

Sorry to trouble but it seems I'm a little lost.
I am currently creating a Shape Calculator for 2D and 3D shapes and I seem to be having a problem with the above mentioned shapes in the title.
Now I have gone about trying to use this particular section of code to get the Area of my Pentagon, I've seen this work elsewhere but can't figure out why it won't work here even after reviewing and comparing my code? I thought someone could possibly point out if this is the correct way to go about solving it or if I've made a mistake I can't see myself? Generally need a second opinion sorry.
double pen = scan.nextDouble();
double penPerm = pen * 5;
double A1 = pen * Math.sqrt(5);
double A2 = 5 + A1;
double A3 = Math.sqrt(5 * A2);
double PenA = (1.0 / 4.0) * A3 * Math.pow(pen, 2);
System.out.println("Your Perimitre is :" + penPerm + "cm and your Area is :" + PenA + "cm Squared");
The other problem I have how should I go about tackling the Hexagon but to be honest, the above Pentagon problem is my main concern before I move onto the Hexagon.
For a regular pentagon you can try the following code:
public static void main(String[] args) {
double side = 10;
double area = (1.0/4.0) * Math.sqrt(5*(5+2*Math.sqrt(5))) * Math.pow(side,2);
System.out.println("Your Perimitre is :" + 5*side + "cm and your Area is :" + area + "cm Squared");
}
You can also try the following code which takes no of sides (n) and edge size (s) as input and computes area for a regular polygon:
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println(" Enter the number of sides in polygon");
int n = input.nextInt();
System.out.println(" Enter the distance between two points");
double s = input.nextDouble();
double area = (n * Math.pow(s, 2)) / (4 * Math.tan(Math.PI / n));
//Print result
System.out.println (" Area is " + area);
System.out.println (" Perimeter is " + s*n);
}

How to go about using tangents in Java?

I was bored and wanted to practice my java coding skills. I made a program that finds the area of a polygon based on what you know (radius, perimeter, apothem).
Here's a portion:
static void pentagon() {
System.out.println("Select what you know");
System.out.println("[1]Perimeter\n[2]Apothem\n[3]Radius");
info = input.nextInt();
if (info == 1) {
System.out.println("What is the perimeter of the pentagon?");
double per = input.nextDouble();
double apothem = per * .137638192;
double answer = apothem * per * .5;
System.out.println("The area of the pentagon is " + answer + " square units.");
} else if (info == 2) {
System.out.println("What is the apothem of the pentagon?");
double apothem = input.nextDouble();
double per = apothem / .137638192;
double answer = apothem * per * .5;
System.out.println("The area of the pentagon is " + answer + " square units.");
} else if (info == 3) {
System.out.println("What is the radius of the pentagon?");
double rad = input.nextDouble();
double per = rad / .1701301617;
double apothem = per * .137638192;
double answer = apothem * per * .5;
System.out.println("The area of the pentagon is " + answer + " square units.");
}
}
Due to the problem that all those decimals (ratio of apothem to perimeter) I had to figure out myself, I could only code a few useful ones.
If I knew how to use tangents, I could figure this out.
Ex: double apothem = length / tan(360/10/2)
(An apothem of a decagon)
Can someone show me how to code the previous line?
The recomended way would be to use java.lang.Math.tan(double a)
double apothem = 1 / java.lang.Math.tan( (2*java.lang.Math.PI)/(10*2))
unless there is some reason why you need extraordinary precision and this does not provide it. Then you may be able to find some third party alternative.
You're looking for the java.lang.Math class which has all the trig functions along with other useful constants like e and PI
So the apothem of a decagon where each side was length long and the equation = length/ 2 tan(180/ n) would be (after importing the Math class by putting at the top of your java file import java.lang.Math; )
EDIT
As user ajb points out, Math.tan() takes radians so you have to convert degrees to radians so you have to use toRadians() to convert from degrees to radians:
double apothem = length / (2 *Math.tan(Math.toRadians(180/10))

Categories