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

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?

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);

computing the volume of a cylinder using repeated addition?

im trying to make a code block to compute the volume of a cylinder but im unfamiliar with java so this is what i had so far. Is there anyway to re-purpose it so i can compute the volume?
package javaapplication231;
import java.util.Scanner;
public class JavaApplication231 {
public static void main(String[] args) {
int a,b,i,product;
Scanner keyboard = new Scanner(System.in);
System.out.print("Enter the values of a and b: ");
a = keyboard.nextInt(); //Line 6
b = keyboard.nextInt();
i=0;
product=0;
while (i<a){
i=i+1;
product= product +b;
}
System.out.println("Product=" + product);
}
}
V =PIr^2h
where r is the radius and h is the height
user input: r and h
PI= 3.1416
To calculate the volume of Cylinder simply evaluate the expression, why do you need loop and do the addition. As you mentioned use the formula to calculate the
Volume=PI*radius2*height
int radius, height;
Scanner keyboard = new Scanner(System.in);
System.out.print("Enter the values of radius and Height: ");
radius = keyboard.nextInt(); // Line 6
height = keyboard.nextInt();
double volume = 3.14 * radius * radius * height;
System.out.println("Volume=" + volume);

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);

Exception in thread "main" java.util.InputMismatchException Using Double

import java.util.Scanner;
import java.text.DecimalFormat;
public class palomba2 {
public static void main(String args[]) {
Scanner keyboard = new Scanner(System.in);
//allows input to be read
// VOLUME OF A PRISM
int prLngth, prWdth, prHght, prVol;
System.out.print("Enter the length of the prism in feet: ");
prLngth = keyboard.nextInt();
System.out.print("Enter the width of the prism in feet: ");
prWdth = keyboard.nextInt();
System.out.print("Enter the height of the prism in feet: ");
prHght = keyboard.nextInt();
prVol = prLngth * prWdth * prHght;
System.out.print("The volume of the prism is " + prVol);
System.out.print(" feet.");
System.out.println("");
System.out.println("");
// AREA/CIRCUMFERENCE OF A CIRCLE
DecimalFormat format = new DecimalFormat("0.##");
double radius, circ, area;
System.out.print("Enter the radius of a circle rounded to the nearest hundreth: ");
radius = keyboard.nextInt();
circ = 2 * radius * 3.14159265358979;
area = radius * radius * 3.14159265358979;
System.out.print("The circumference of the circle is " + format.format(circ));
System.out.print("units.");
System.out.print("The area of the circle is " + format.format(area));
System.out.print("units.");
}
}
Everything in my code works up until I input the radius of the circle. It comes up with the error messages after I input a radius.
Enter the radius of a circle rounded to the nearest hundreth: 2.12
Exception in thread "main" java.util.InputMismatchException
at java.base/java.util.Scanner.throwFor(Scanner.java:860)
at java.base/java.util.Scanner.next(Scanner.java:1497)
at java.base/java.util.Scanner.nextInt(Scanner.java:2161)
at java.base/java.util.Scanner.nextInt(Scanner.java:2115)
at palomba2.main(palomba2.java:52)
You type a double variable 2.12 but trying to read it as an int : input mismatch -> InputMismatchException
Also, the variable radius is of type double so you cannot give to it an int value (also you, you need :
radius = keyboard.nextDouble();
But still better to use radius = Double.parseDouble(keyboard.nextLine()) to consume the return char (same for int : int val = Integer.parseInt(keyboard.nextLine())
You just have to change the:
radius = keyboard.nextInt();
to:
radius = keyboard.nextDouble();
because the variable radius is declared as double
it should look like this:
double radius, circ, area;
System.out.print("Enter the radius of a circle rounded to the nearest hundreth: ");
radius = keyboard.nextDouble();

For loop to figure out how much paint to buy to paint a room

These are the instructions that I was given to make my program:
Write a program that calculates the number of buckets of paint to use for a room and the optimal number of cans to
purchase.
You need to ask the height of the room and the length and width of the room. The room is rectangular. You must
paint the walls and the ceiling but not the floor. There are no windows or skylights. You can purchase the following
size buckets of paint.
• 5-liter bucket costs $15 each and covers 1500 square feet.
• 1-liter bucket costs $4 and covers 300 square feet.
I already have most of my code written, I just need help figuring out how many buckets to buy using the for loop.
Here is my program:
public class BrandonLatimerS5L1TryItSolveIt7 {
public static void main(String[] args){
//declares variables
double length;
double width;
double height;
double ceilingArea;
double wallsArea;
//initializes bucket variables
int fiveLiterBucket = 1500;
int oneLiterBucket = 300;
//Prompts user and gets input for length
Scanner input = new Scanner(System.in);
System.out.println("Please enter the length of the room in feet: ");
length = input.nextDouble();
//prompts user and gets input for width
System.out.println("Please enter the width of the room in feet: ");
width = input.nextDouble();
//Prompts for input and gets input for height
System.out.println("And lastly, please enter the height of the room in feet: ");
height = input.nextDouble();
//figures out the total area that needs to be painted
ceilingArea = length * width;
wallsArea = (2 * (width * height) + (2 * (length * height)));
double totalArea = ceilingArea + wallsArea;
//For loop to figure out how much paint will be needed.
for(int numOfBuckets = 0; totalArea > 1; numOfBuckets++){
totalArea = totalArea - (totalArea / 1500);
System.out.println("You will need " + numOfBuckets + " buckets.");
continue;
/*
* This program taught me to use the for loop. I just can't seem to figure out how to find the amount of paint I need to buy.
*/
}
}
Any help is greatly appreciated!
A for loop is not the right tool for this job. Use a for loop when your program knows in advance how many times the loop should execute. If you don't know, or can't calculate how many times the loop body should execute, use a while loop, or in this case, just use arithmetic.
Just do these steps in sequence, without using a loop:
Divide the total area by 1500 to find out how many big buckets of paint to buy.
Multiply that number by 1500 to find out the area that will be covered.
Subtract that area from the total area to find out how much blank wall space is left over.
Divide the left over wall space by 300 to find out how many small buckets to buy.
Use the same method as above to decide if you need an extra small bucket for any remaining blank wall space.

Categories