Using a while loop to declare an impossible output - java

import java.io.*;
import java.util.*;
public class volumeConeD
{//class
public static void main (String [] args)
{//main
Scanner keyBoard = new Scanner(System.in);//input for keyBoard
//variables
double volume;
double radius;
double hieght;
double pie = 3.14;
double yes = 1.0;
boolean volumeTwo = true;
while(volumeTwo == 0){
System.out.print("Volume of a Cone... V=1/3(3.14)r^2(h)");
System.out.println ();
System.out.println ();
radius = getRadius(radius); //call to method
System.out.print("Enter a Height ");
hieght = keyBoard.nextDouble ();
//math
volume = .33333 * pie * radius * radius * hieght;
System.out.printf ("Volume = " + volume);
}//end of while
}//end of main
public static double getRadius (double radius)
{
Scanner keyBoard = new Scanner(System.in);
System.out.print("Enter Radius Squared Number ");
radius = keyBoard.nextDouble ();
return radius;
}
}//end of program
So here is my issue. I have to write this so that if the answer ends up being Volume = 0 the program must end. I have to use a while loop and that method to input the radius. I keep getting this error and I can't figure out why.
error
volumeConeD.java:25: error: incomparable types: boolean and int
while(volumeTwo == 0){
^
1 error.
I understand what the error means but I cannot figure out how to fix it. Please help
NEW EDIT...also in the while loop it must read, while(Volume == 0).

Use while(volumeTwo) if you want it to continue while volumeTwo is true or while(!volumeTwo) if you want it to continue while volumeTwo is false.

You try to compare boolean with 0 , Hmm
boolean volumeTwo = true;
while(volumeTwo == 0)
Use
while(volumeTwo == true)
or
while(volumeTwo)

Get rid of volumeTwo altogether. You care if volume is 0, so just change the while loop to while(volume!=0) and make sure the volume is initialized to something besides 0.

I think following is what you are trying to achieve. Please see the comments which are marked with <======= :
...
//boolean volumeTwo = true; // <======= Manoj - COMMENT THIS LINE
double volumeTwo = 1.0; // <=========== Manoj - any non-zero for that matter
...
...
while(volumeTwo != 0.0){
...
//math
volume = .33333 * pie * radius * radius * hieght;
System.out.printf ("Volume = " + volume);
volumeTwo = volume; // <=============== Manoj - update volumeTwo with calculated volume
// <=============== - when volumeTwo becomes 0.0 loop quits

Related

Java Box program with multiple methods and constructors

I am a beginner in programming and am having trouble with using constructors, specifically. I have to write a program for one of my labs that must consist only of:
Three instance variables – length, width and height (each of type double)
One instance variables – input (type Scanner) initialized to System.in
Default constructor (no-arg) – initialize all three instance variables to 1
Initial constructor – initialize all three instance variables
Copy constructor – copy Box
inputWidth, inputLength, and inputHeight methods that set the instance variables based on user input have not parameters and do not
return a value.
a displayDimensions method that displays the length X Width X height (separated by “X”) and does not return a value.
a calcVolume method that has no parameters and calculates the volume of the box
We also were given application BoxTest in which the output must
exactly match the following:
Default dimensions are 1.0 X 1.0 X 1.0 with volume of 1.0
Initial dimensions are 8.5 X 11.0 X 1.0 with volume of 93.5
Copied dimensions are 8.5 X 11.0 X 1.0 with volume of 93.5
Update dimensions
Enter length: 1
Enter width: 2
Enter height: 3
Updated dimensions are 1.0 X 2.0 X 3.0 with volume of 6.0
Here's my code:
import java.util.Scanner;
public class Box {
public static void main(String args[]) {
double length, width, height;
Scanner input=new Scanner(System.in);
new Box() { //
Box defaultBox=new Box();
double length = 1.0;
double width = 1.0;
double height = 1.0;
System.out.print("Default dimensions are " + length + " X " + width + " X " + height);
defaultBox.displayDimensions();
System.out.println(" with volume of "+defaultBox.calcVolume());
Box initialBox=new Box(length, width, height);
length = 8.5;
width = 11.0;
height = 1.0;
System.out.print("Initial dimensions are " + length + " X " + width + " X " + height);
initialBox.displayDimensions();
System.out.println(" with volume of "+initialBox.calcVolume());
Box copyBox=new Box(initialBox);
System.out.print("Copied dimensions are " + length + " X " + width + " X " + height);
copyBox.displayDimensions();
System.out.println(" with volume of "+copyBox.calcVolume());
System.out.println("\nUpdate dimensions");
initialBox.inputLength();
initialBox.inputWidth();
initialBox.inputHeight();
System.out.print("Updated dimensions are ");
initialBox.displayDimensions();
System.out.println(" with volume of "+initialBox.calcVolume());
}
double inputLength() {
Scanner input;
double length = input.nextDouble();
}
double inputWidth() {
Scanner input;
double width = input.nextDouble();
}
double inputHeight() {
Scanner input;
double height = input.nextDouble();
}
double displayDimensions(double length, double width, double height) {
Scanner input;
}
double calcVolume() {
}
}
What am I missing? My program will not compile and gives the error message
Exception in thread "main" java.lang.Error: Unresolved compilation problems:
Syntax error, insert "Identifier (" to complete MethodHeaderName
Syntax error, insert ")" to complete MethodDeclaration
Syntax error, insert ";" to complete MethodDeclaration
Syntax error, insert "}" to complete ClassBody
at Box.main(Box.java:18)
As I said in the comments, you have put everything in main. Don't do that. As it is, your Box class is basically empty, and you are currently almost creating an anonymous sub-class in main. Your directions do not mention a main, but are pretty straight forward. You were supposed to write something like
public class Box {
// Three instance variables – length, width and height (each of type double)
private double length, width, height;
// One instance variables – input (type Scanner) initialized to System.in
private Scanner input = new Scanner(System.in);
// Default constructor (no-arg) – initialize all three instance variables to 1
public Box() {
this.length = this.width = this.height = 1;
}
// Initial constructor – initialize all three instance variables
public Box(double length, double width, double height) {
this.length = length;
this.width = width;
this.height = height;
}
// Copy constructor – copy Box
public Box(Box b) {
this(b.length, b.width, b.height);
}
// inputWidth, inputLength, and inputHeight methods that set the instance
// variables based on user input have not parameters and do not return a value.
public void inputWidth() {
this.width = input.nextDouble();
}
public void inputLength() {
this.length = input.nextDouble();
}
public void inputHeight() {
this.height = input.nextDouble();
}
// a displayDimensions method that displays the length X Width X height
// (separated by “X”) and does not return a value.
public void displayDimensions() {
System.out.printf("%.2fX%.2fX%.2f%n", length, width, height);
}
// a calcVolume method that has no parameters and calculates the volume of the
// box
public double calcVolume() {
return length * width * height;
}
}
Create a Java class named Package that contains the following:
Package should have three private instance variables of type double named length, width, and height.

Java :calculate the circle diameter and

this is a Class about calculating diameter,circumference and area of circle that user enter radius value and it gives him diameter,cirucumf... ,
this is the class code:
package circle;
import java.util.Scanner;
public class Circle {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int radius=0;
int diameter;
int circumference ;
int area;
int Pi;
Pi=(int) 3.14;
area = (int) (radius*radius*Pi);
circumference =(int)(radius*2*Pi);
diameter = (int)(radius*2);
System.out.print("Enter radius value:");
radius=input.nextInt();
System.out.printf("area is %d%n" , area);
System.out.printf("diameter is %d%n", diameter);
System.out.printf("circumference is %d%n", environment);
}
}
this is what output gives me :
Enter radius value: (for exmaple) 4
area is 0 // (real value is 50.24)
diameter is 0 // (8)
circumference is 0 //(25.12)
what is the code problem?
or how can i fix it?
You read the radius AFTER computing area/environment(?)/diameter. Furthermore, your values are int variables, which also means that your value for pi is just 3. I suggest you correct the order of the statements, and start using double instead of int.
"environment" will be replaced by "circumference". As your excepted output is decimal value. So use float/double instead of int. In your program you are calculating diameter,circumference and area after initialising the value of radius(radius=0) but before getting the value of radius(radius=4). I have modified your code. It seem help you.
package circle;
import java.util.Scanner;
public class Circle {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int radius=0;
float diameter;
double circumference ;
double area;
double Pi;
Pi= 3.14;
System.out.print("Enter radius value:");
radius=input.nextInt();
area = (radius*radius*Pi);
circumference =(radius*2*Pi);
diameter = (radius*2);
System.out.printf("area is " + area);
System.out.printf("\ndiameter is "+ diameter);
System.out.printf("\ncircumference is "+ circumference);
}
}
you are calculating the values area/environment(?)/diameter before getting and initializing the radius input.And at that time the default value for radius is set which is 0. Hence it is giving the results of all the parameters as 0.So you will have to re order your code as below:
System.out.print("Enter radius value:");
radius=input.nextInt();
area = (int) (radius*radius*Pi);
environment=(int)(radius*2*Pi);
diameter = (int)(radius*2);
System.out.printf("area is %d%n" , area);
System.out.printf("diameter is %d%n", diameter);
System.out.printf("environment is %d%n", environment);

Law of Cosines used to find third side - why do I get the wrong result?

I keep getting an output of 45.8 when inputting 24 as side a, 32 as side b and 115 as angle C
the correct angle is 47.4
can someone please tell me what I'm doing wrong?
The calculation is:
double sideC = Math.sqrt((Math.pow(sideA, 2) + Math.pow(sideB, 2))- 2*(sideA*sideB)*(Math.cos(angleC)));
Source:
import java.util.Scanner;
public class TriangleCalc
{
/**
* #param args
*/
public static void main(String[] args)
{
System.out.println(" Triangle Calculator ");
Scanner inputab = new Scanner(System.in);
System.out.println("Input lenghts of sides 'a' and 'b':");
double sideA = inputab.nextDouble();
double sideB = inputab.nextDouble();
System.out.println("Input the size of Angle C in degrees:");
double angleC = inputab.nextDouble();
inputab.close();
double sideC = Math.sqrt(((Math.pow(sideA, 2) +
Math.pow(sideB, 2))- (2*(sideA*sideB)*(Math.cos(angleC)))));
System.out.println("\t /\\\n\t / \\\n\t / \\\n\t/ \\");
System.out.printf(" %3.1f",sideA);
System.out.print("/ \\");
System.out.printf("%3.1f",sideB);
System.out.println("\n / \\\n / \\\n"
+" /______________\\");
System.out.print(sideC);
As the Javadoc for Math.cos states
a - an angle, in radians.
You need to convert degrees to radians if you want to use degrees.
You can use Math.toRadians
Replace
Math.cos(angleC)
with
Math.cos(Math.toRadians(angleC))

NEED HELP: Computer Science 101 HW/JAVA

I wrote the code for the problem in the title and seem to be having some problems. Can anyone give me some insight or tips on what I am doing wrong. Especially with the "if...else" portion of the code.
Here is the question #9. If you click the link it will show you a printout of the question.
http://s21.postimg.org/nl2tmf5tj/Screen_Shot_2013_09_21_at_6_44_46_PM.png
Here is my code:
import java.util.*;
public class Ch3Ex9 {
/**
* This method asks user for an x,y coordinates of a point, then returns the
* distance to the origin and which quadrant the point is in.
*/
public static void main(String[] args)
{
double xCoord; //x Coordinant initialized to 3
double yCoord; //y Coordinant initalized to 4
double hypo; //hypotenuse
//declare an instance of Scanner to read the datastream from the keyboard
Scanner keyboard = new Scanner(System.in);
//get x Coordinant from the user
System.out.print("Please enter the X coordinant: ");
xCoord = keyboard.nextDouble();
//get Y Coordinate from the user
System.out.print("Please enter the Y coordinant: ");
yCoord = keyboard.nextDouble();
//calculate the hypotenuse which is the length to the origin
hypo = Math.hypot(xCoord, yCoord);
System.out.println("The hypotenuse is "+hypo);
//determine which Quadrant the user-inputted point resides in
if (xCoord>0) && (yCoord>0) //
System.out.println("Point lies in Quadrant I.");
else if (xCoord<0) && (yCoord>0)
System.out.println("Point lies in Quadrant II.");
else if (xCoord<0) && (yCoord<0)
System.out.println("Point lies in Quadrant III.");
else if (xCoord>0) && (yCoord<0)
System.out.println("Point lies in Quadrant IV.")
}
}
There are too many parentheses. Change
if (xCoord>0) && (yCoord>0)
to
if (xCoord>0 && yCoord>0)
and similarly with the others.

How to read a word and then take in input in java?

So im trying to create a program that reads a word and then sets of 2 numbers that specify rectangles, (w, h). When the word is “area”, the program should calculate and print the area. When the word is “perimeter”, calculate and print the perimeter. When the word is “quit”, exit the program, without reading the numbers.
The output should look like this:
java Rectangle1
? area 1 2 3 4
Area of (1.0, 2.0, 3.0, 4.0) = 12.0
? perimeter 1 2 3 4
Perimeter of (1.0, 2.0, 3.0, 4.0) = 14.0
? area 2 3 4 5
Area of (2.0, 3.0, 4.0, 5.0) = 20.0
? quit
$
I am able to get the program to store the width and height first and then read area or perimeter so it looks like this:
$ java Rectangle1
To find the area or perimeter of a rectangle
Enter the Length from 1 to 20 ( defaults to 1 ) :
1
Enter the Width from 1 to 20 (defaults to 1 ) :
2
Enter 1 to find Area
Enter 2 to find Perimeter
Enter 3 to quit
Choice:1
Area: 2.000000
Choice:2
Perimeter: 6.000000
Choice:3
But I am not sure how to get it to read the word area or perimeter and then on the same line take in the width and height then print out either area of perimeter as an answer until quit is entered
Here is my code:
import java.util.Scanner;
//I just put everything in one class, the Rectangle keeps its properties
//main was just added
//feel free to seperate as needbe
public class Rectangle1{
// length and width default to 1
private static double length;
private static double width;
public static double perimeter;
public static double area;
private static int choice;
//empty constructor
public Rectangle1(){
setLength(1);
setWidth(1);
}
// constructor with length and width supplied
public Rectangle1( double theLength, double theWidth) {
setLength(theLength );
setWidth( theWidth );
} // end Rectangle two-argument constructor
// validate and set length
public void setLength(double theLength){
length = ( theLength > 0.0 && theLength < 20.0 ? theLength : 1.0 );
} // end method setLength
// validate and set width
public void setWidth(double theWidth){
width = ( theWidth > 0.0 && theWidth <20.0 ? theWidth : 1.0);
}//end method setWidth
// get value of length
public double getLength(){
return length;
}//end method getLength
// get value of width
public double getWidth(){
return width;
}// end method getWidth
// calculate rectangle's perimeter
public static double calcPerimeter () {
perimeter = (length * 2) + (width * 2);//calculates area
System.out.printf("Perimeter: %f\n", perimeter);//output
return perimeter;
}
// calculate rectangle's area
public static double calcArea(){
area = (length * width);//calculates area
System.out.printf("Area: %f\n", area);//output
return area;
}
// convert to String
public String toString(){
return String.format("%s02d %02d", length, width);
}///end method toPerimeter String
public static void main( String args[] )
{
Scanner input = new Scanner (System.in);
Rectangle1 myRectangle = new Rectangle1(); //this is an object instance
int choice;
double width;
double length;
System.out.println("To find the area or perimeter of a rectangle" );
System.out.println ( "Enter the Length from 1 to 20 ( defaults to 1 ) : " );
length = input.nextInt();
System.out.println ( "Enter the Width from 1 to 20 (defaults to 1 ) : " );
width = input.nextInt();
//We need to now set these values to our rectangle object
myRectangle.setWidth(width);
myRectangle.setLength(length);
System.out.println ( "Enter 1 to find Area" );
System.out.println ( "Enter 2 to find Perimeter" );
System.out.println ( "Enter 3 to quit" );
System.out.printf ( "Choice:" );
choice = input.nextInt();
while ( choice != 3 ){
if (choice == 1){
System.out.printf( "", myRectangle.calcArea()); //call the method of our created object instance, NOT the class name
}
else if ( choice == 2){
System.out.printf( "", myRectangle.calcPerimeter());//call the method of our created object instance, NOT the class name
}
System.out.printf ( "Choice:" );
choice = input.nextInt();
}
}//end method main
} // end class Rectangle
Sorry I know the indenting is not good.
You need to ask the user for the entire question (ie {cmd} {x} {y} {width} {height}) in one go and then parse this result.
For example...
cmd = input.nextLine(); // Where cmd is a String
// example input...
// area 1 2 3 4
// perimeter 1 2 3 4
// quite
Once you have the user input, you need to parse this text. Now you could use regular expression to do this, but better to keep it simple to start with ;)
Instead, you can use what you already know...
Scanner parser = new Scanner(cmd);
cmd = parser.next();
// Example output...
// area
// perimeter
// quite
Now. Once you have the cmd (or what the user wants to do), you need to start making decisions about...
// First, we need to know if we can handle the question...
if ("area".equalsIgnoreCase(cmd) || "perimeter".equalsIgnoreCase(cmd)) {
Then you can start extracting the parameters...
int x = parser.nextInt();
int y = parser.nextInt();
int width = parser.nextInt();
int height = parser.nextInt();
Now, I'd probably also use parser.hasInt() to check that the parameter exists, but that's just me...
Then, all you need to do is stick it inside a loop...
do {
// Get user input
// Parse user input
// Make some decisions...
} while (!"quite".equalsIgnoreCase(cmd));

Categories

Resources