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.
Related
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);
Hello I was given an assignment in which I have to draw 3 circles using their center coordinates and radii which is input by the user. The assignment specifically states that I must input the center and radius, I cannot input the x and y coordinates separately. They must be input as a coordinate pair (x,y).
Here is my code.
import java.util.*;
import java.awt.*;
public class Circles {
static Scanner CONSOLE = new Scanner(System.in);
public static final int PANEL_HEIGHT = 300;
public static final int PANEL_WIDTH = 400;
public static void main (String [] args) {
DrawingPanel panel = new DrawingPanel (PANEL_HEIGHT,PANEL_WIDTH);
Graphics g = panel.getGraphics();
System.out.println();
System.out.println("Red Circle data");
System.out.println("Input center of Red circle: ");
int center1 = CONSOLE.nextInt();
System.out.println("Input radius of Red circle: ");
int radius1 = CONSOLE.nextInt();
System.out.println();
System.out.println("Blue Circle data");
System.out.println("Input center of Blue circle: ");
int center2 = CONSOLE.nextInt();
System.out.println("Input radius of Blue circle: ");
int radius2 = CONSOLE.nextInt();
System.out.println();
System.out.println("Green Circle data");
System.out.println("Input center of Green circle: ");
int center3 = CONSOLE.nextInt();
System.out.println("Input radius of Green circle: ");
int radius3 = CONSOLE.nextInt();
g.setColor(Color.RED);
g.fillOval(center1 -radius1 ,center1-radius1 ,radius1 ,radius1);
g.setColor(Color.BLUE);
g.fillOval(center2 -radius2 ,center2-radius2 ,radius2 ,radius2);
g.setColor(Color.GREEN);
g.fillOval(center3 -radius3 ,center3-radius3 ,radius3 ,radius3);
}
}
My problem is that I don't know how to properly input and store the x and y coordinates as one int. As it is currently it just takes one data point, stores it, and uses it as both x and y.
System.out.println("Red Circle data");
System.out.println("Input center of Red circle: ");
int center1 = CONSOLE.nextInt();
Here is where I know java must ask for the values and store them.
g.setColor(Color.RED);
g.fillOval(center1(THIS SHOULD BE THE X VALUE) -radius1 ,center1(THIS SHOULD BE THE Y VALUE)-radius1 ,radius1 ,radius1);
Then here I have to somehow get the values stored in the above code.
Please help i'm kinda new to this and would appreciate some feedback! Hope what im asking for makes sense! :S
not JAVA coder so I stick to C++, are you sure you want x,y pair in single int ?
1.If yes then just encode it
but you must know the coordinates range
for example if you have 32bit int and your coordinates fits to 16 bit then:
int xy,x,y;
xy = (x&0x0000FFFF) | ((y&0x0000FFFF)<<16); // this makes xy = (x,y)
and if you need to unpack the data again do:
x= xy &0x0000FFFF; if (int(x&00008000)!=0) x|=0xFFFF0000;
y=(xy>>16)&0x0000FFFF; if (int(y&00008000)!=0) y|=0xFFFF0000;
the if at the end restores the missing bits for negative numbers ...
2.if in single variable instead then use
array:
int xy[2],x,y;
xy[0]=x;
xy[1]=y;
or struct/class
int x,y;
struct point { int x,y; } xy;
xy.x=x;
xy.y=y;
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
I took a course in Java at University 2 years ago, and now I have to create a program where I type in 2 sets of coordinates, and the program will create a 3-D graph of a line between the two points, as well as give the slope and angle between the two points. I've been trying to get familiar with the Java syntax, but I have to complete the program by tomorrow so I figured I would ask for help. I've put together code, but it isn't in Java's syntax (most of it isn't anyways) and I need help converting it to code that will work. I'm using double for my variables because the points the user enters can be decimals. The only coordinates the user will input are x, and y for both points, the z coordinates are set at z1 = 0 and z2 = 1. The way I have put the variables together, it assumes the z coordinate extends out of the screen, and the y coordinate plane extends vertically.
Again, I'm familiar with general coding terms, but as I've looked around on the internet i see things like import java.util.* and that stuff doesn't make sense to me as to how it owuld apply to my program.
Any help is appreciated!
P.S. if you want me to add comments as to what certain things mean, let me know. i.e. create.cube is a syntax I made up, but I want the computer to create a window that will show a cube with the coordinates I have set.
package slope;
public class Slope1 {
double xvar;
double yvar;
double zvar;
xvar x1 = new xvar;
xvar x2 = new xvar;
yvar y1 = new yvar;
yvar y2 = new yvar;
zvar z1 = new zvar;
zvar z2 = new zvar;
public static void main(String[]args){
z1 = 0;
z2 = 1;
get.x1 from user
if(x1>9 or x1<-9){
System.out.println("Please choose values within range")
}
get.x2 from user
if(x2>9 or x2<-9){
System.out.println("Please choose values within range")
}
get.y1 from user
if(y1>12 or y2<-12){
System.out.println("Please choose values within range")
}
get.y2 from user
if(y1>12 or y2<-12){
System.out.println("Please choose values within range")
}
slope1 = (y2-y1)/(x2-x1);
angle1 = arctan(slope1);
distance1 = (y2-y1)/sin(angle1);
slopeFinal = 1/distance1;
angleFinal = arctan(slopeFinal);
System.out.println("Your Slope is " + slopeFinal);
System.out.println("Angle of entry is " + angleFinal);
}
public static void main(String[]args){
create.cube;
xlength cube = -9 to 9;
ylength cube = -12 to 12;
zlenght cube = 0 to 12
cube x origin at x=0;
cube y origin at y=0;
cube z origin at z=0;
draw line from (x1,y1,z1) to (x2,y2,z2) in cube;
}
}
Your code doesn't make sense at all and has many syntax errors.
create.cube;
xlength cube = -9 to 9;
ylength cube = -12 to 12;
zlenght cube = 0 to 12
cube x origin at x=0;
cube y origin at y=0;
cube z origin at z=0;
None of the above are valid statements in java.
You might wanna study java once again and look up gui programming with java as well. below is a link to get you started.
https://en.wikibooks.org/wiki/Java_Programming
I am completely lost on how to sort the coordinates into an array, and find the distance between them. This is the question:
Create a new class called “Circle” that can be used to create customized circle objects. Your class should include the following – be sure to comment your class appropriately:
double radius,
double xPosition,
double yPosition, and
A method that computes the distance from the xPosition and yPosition of one circle to the xPosition and yPosition of another circle. Use the standard distance formula to compute this value. You only need to compute the distance from center point to center point for the purposes of this method. Here’s a method header to get you started:
public double distanceFrom(Circle test)
Create a new class called “Assignment06b”. Do the following in this class:
Prompt the user to enter in a number of circles (i.e. How many circles do you want to create?)
Next, ask the user to enter in the radius, xPosition and yPosition for each circle. Store their input in an array of Circles of the appropriate size.
Finally, Iterate through your array and display distance information for each circle. Ensure that you do not calculate the distance from a given circle back to itself (i.e. no need to compute distance between circle #1 and circle #1) — Here’s a sample running of your program.
Here's what I have so far:
import java.util.Scanner;
public class Assignment06b
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
System.out.print("How many circles do you want to create?:");
int amount = input.nextInt();
int[] arrayX = new int [amount];
int[] arrayY = new int [amount];
int counter = 0;
for (counter = 0; counter < amount; counter++)
{
System.out.println("Enter info for Circle #" + (counter + 1));
System.out.print("Radius: ");
double width = input.nextDouble();
System.out.print("X Position: ");
arrayX[counter] = input.nextInt();
System.out.print("Y Position:");
arrayY[counter] = input.nextInt();
}
}
class Circle
{
double radius;
double xPosition;
double yPosition;
Circle(double radius, double xPosition, double yPosition)
{
}
public double distanceFrom(Circle test)
{
double equation = (xPosition-xPosition)*(xPosition-xPosition) + (yPosition-yPosition)*(yPosition-yPosition);
double answer = Math.pow(equation, 0.5);
return answer;
}
}
}
You are tip toeing around object orientation here, change things around so you have an array of circles instead of an array of ints:
Circle[] arrayCircles = new Circle [amount];
Also you aren't setting any values in your circle class, you probably want to fix that:
Circle(double radius, double xPosition, double yPosition)
{
this.radius = radius;
this.xPosition = xPosition;
this.yPosition = yPosition;
}
Then you can add circles to your collection like this:
arrayCirles[0] = new Circle(myRadius, myXPosition, myYPosition);
and call your distanceFrom method call like so:
//Obviously do this in a loop of some kind and make sure they exist first
arrayCircles[0].distanceFrom(arrayCircles[1]);
Hopefully the rest you can figure out yourself
(Also take another look at your distanceFrom method, you want to compare the circle you are passed as a parameter, not to yourself)
I'm currently working in a CAD software and I needed to find the distance between circles (holes) in a square grid/array and I found that the distance between circles (edge to edge) is given by
(L-N*D)/(N-1)
Where:
L is the distance of the array from edge to edge (not to the centre point of the end circle but to the edge of end circles)
N is the number of circles
D is the diameter of the circles
if you measure L from centre point to centre point
(L+D-N*D)/(N-1)
If you're looking to find the distance between centre point to centre point I'm sure you can derive it
Bit late but hopefully this helps someone else!