The following program contains 9 errors. Correct the errors and submit a working version of the program. The corrected version of the program should produce the following output:
x = 10.01 and y = 8.0
x = 10.01 and y = 867.5309
The value from main is: 867.5309
z = 5
I have already made some changes on this, but I can figure out why x and y aren't being called.
public class Oops3 {
public static void main(String[] args) {
double y = 867.5309;
double x = 10.01;
printer(double x, double y);
printer(x);
printer(y);
System.out.println("z = " + z);
}
public static void printer(double x, double y) {
int z = 5;
System.out.println("x = " + double x + " and y = " + double y);
System.out.println("The value from main is: " + y);
}
}
Try this: (not sure how many changes you have already made)
public class Oops3 {
public static void printer(double x, double y) {
System.out.println("x = " + x + " and y = " + y);
System.out.println("The value from main is: " + y);}
public static void main(String[] args) {
int z = 5;
double y = 867.5309;
double x = 10.01;
System.out.println("x= " + x + " and y = 8.0");
printer( x,y);
System.out.println("z = " + z);
}}
The following is the working code you are looking for
public class Oops3
{
public static void printer(double x, double y, int z) {
System.out.println("x = " + x + " and y = " + y);
System.out.println("The value from main is: " + y);
System.out.println("z = " + z);
}
public static void main(String[] args) {
Oops3 O=new Oops3();
double y = 867.5309;
double x = 10.01;
int z = 5;
O.printer(x, y, z);
}
}
This code worked for me
public class Oops3 {
public static void main(String [] args) {
Oops3 i = new Oops3();
double bubble = 867.5309;
double x = 10.01;
double y = 8.0;
int z = 5;
i.printer(x, y);
i.printer(x, bubble);
System.out.println("The value from main is: " + bubble);
System.out.println("z = " + z);
}
public static void printer(double x, double y) {
System.out.println("x = " + x + " and y = " + y);
}
}
Related
I've been trying to make a calculator with multiple classes, however I am not that experienced with classes. I've been having a few problems with it.
This is the user input section, doesn't seem to have any problems
import java.util.Scanner;
public class GetData
{
Scanner getdata = new Scanner(System.in);
public double intx;
public double inty;
public void getInt() {
System.out.print("Enter a number");
double intx = getdata.nextDouble();
}
public void getDouble() {
System.out.print("Enter a double");
double inty = getdata.nextDouble();
}
}
This is the operation section, the most problems are happening here, as said in the error there are missing error statements,
import java.util.Scanner;
public class MathOps {
Scanner mathops = new Scanner(System.in);
public double x;
public double y;
public double answer;
public double add(int x, int y) {
System.out.println("Adding " + x + "and " + y);
return x + y;
}
public double multiply(int x, int y) {
System.out.println("Multiplying " + x + "and " + y);
return x * y;
}
public double sub(int x, int y) {
System.out.println("Subtracting " + x + "and " + y);
if(x >= y) {
return x - y; }
if(y >= x) {
return y - x; }
}
public double divide() {
System.out.println("Dividing " + x + "and " + y);
if(x >= y) {
return x / y; }
if(y >= x) {
return y / x; }
}
}
----jGRASP exec: javac -g MathOps.java
MathOps.java:27: error: missing return statement
}
^
MathOps.java:35: error: missing return statement
}
^
2 errors
----jGRASP wedge2: exit code for process is 1. ----jGRASP: operation
complete.
This is were I'm putting it all together, also has problems
import java.util.Scanner;
public class L3Operations
{
int geti;
int getd;
public static void main(String[] args)
{
//instatnate
Scanner a = new Scanner(System.in);
int x1 = a.getInt();
}
}
As an aside, note that x >= y and y >= x will both be false if either x or y is Double.NaN, and you can enter NaN as a double value with the Scanner class (and also Infinity and -Infinity). So unless you check the values ahead of time to rule out NaN, you can't assume that at least one of those two conditions will be true.
You also can't assume that if x > y and y > x are false, then x == y, because for NaN all of those are false. And, if you have just assigned x = y, you can't assume that x == y, since if y was NaN that will be false.
I have forgot my old math from school.
I have made a java class that should calculate the distance between x,y,z and origo.
But have made a wrong turn somewhere and cant get out…
The calculation doesnt give the right value.
Can you help see what i have made wrong?
Its the distance calculation thats is wrong.
import java.io.PrintWriter;
import java.util.Scanner;
public class U3point {
public static void main(String[] args) {
System.out.println("Values for x,y,z ");
Scanner in = new Scanner(System.in);
PrintWriter out = new PrintWriter(System.out, true);
double x;
double y;
double z;
String[] koordinater = null;
String rad = in.nextLine();
System.out.println("toString: ");
while (!rad.equals("")) {
koordinater = rad.split("\\s");
x = Double.parseDouble(koordinater[0]);
y = Double.parseDouble(koordinater[1]);
z = Double.parseDouble(koordinater[2]);
Point p = new Point(x, y, z);
out.println(p);
//call method for origo
double d = p.getDistanceToOrigo();
System.out.println("Distance to Origo: " + d);
// call method for distance
double d2 = p.getDistanceTo(x, y, z);
System.out.println("Distance: " + d2);
System.out.println("Exit program with enter or new values");
rad = in.nextLine();
}
}
}
class Point {
private double x;
private double y;
private double z;
public Point(double x1, double y1, double z1) {
x = x1;
y = y1;
z = z1;
}
public void setX(double x1) {
x = x1;
}
public void setY(double y1) {
y = y1;
}
public void setZ(double z1) {
z = z1;
}
public double[] getCoordinates() {
return new double[] { x, y, z };
}
public double getDistanceTo(double x2, double y2, double z2) {
return (Math.sqrt(((x - x2) * (x - x2) + (y - y2) * (y - y2) + (z - z2) * (z - z2))));
}
public double getDistanceToOrigo()
{
return (Math.sqrt(((x) * (x) + (y) * (y) + (z) * (z))));
}
public String toString() {
return " x: =" + x + " y:=" + y + " z:=" + z;
}
}
The problem you were calculating the distance to the same point, here are an approach for you:
import java.io.PrintWriter;
import java.util.Scanner;
public class U3point {
public static void main(String[] args) {
PrintWriter out = new PrintWriter(System.out, true);
Point p1 = generatePoint();
Point p2 = generatePoint();
out.println(p1);
out.println(p2);
// call method for origo double
double d = p1.getDistanceToOrigo();
System.out.println("Distance to Origo: " + d);
// call method for distance
double d2 = p1.getDistanceTo(p2.getX(), p2.getY(), p2.getZ());
System.out.println("Distance: " + d2);
System.out.println("The end!!");
}
public static Point generatePoint() {
System.out.println("Values for x,y,z ");
String[] koordinater = null;
Scanner in = new Scanner(System.in);
String rad = in.nextLine();
Point p = null;
koordinater = rad.split("\\s");
double x = Double.parseDouble(koordinater[0]);
double y = Double.parseDouble(koordinater[1]);
double z = Double.parseDouble(koordinater[2]);
p = new Point(x, y, z);
return p;
}
}
class Point {
private double x;
private double y;
private double z;
public Point(double x1, double y1, double z1) {
x = x1;
y = y1;
z = z1;
}
public void setX(double x1) {
x = x1;
}
public void setY(double y1) {
y = y1;
}
public void setZ(double z1) {
z = z1;
}
public double getX() {
return x;
}
public double getY() {
return y;
}
public double getZ() {
return z;
}
public double[] getCoordinates() {
return new double[] { x, y, z };
}
public double getDistanceTo(double x2, double y2, double z2) {
return (Math.sqrt(((x - x2) * (x - x2) + (y - y2) * (y - y2) + (z - z2) * (z - z2))));
}
public double getDistanceToOrigo() {
return (Math.sqrt(((x) * (x) + (y) * (y) + (z) * (z))));
}
public String toString() {
return " x: =" + x + " y:=" + y + " z:=" + z;
}
}
Output:
Values for x,y,z
1 2 3
Values for x,y,z
4 5 6
x: =1.0 y:=2.0 z:=3.0
x: =4.0 y:=5.0 z:=6.0
Distance to Origo: 3.7416573867739413
Distance: 5.196152422706632
The end!!
public class Vector {
private int x, y, z;
public Vector(int x, int y, int z) {
this.x = x;
this.y = y;
this.z = z;
}
public void add(Vector v) {
x += v.x;
y += v.y;
z += v.z;
}
public void silly(int x, int y, int z) {
this.x = ++x;
this.y = y + 1;
this.z += z;
}
public int getX() {
return x;
}
public int getY() {
return y;
}
public int getZ() {
return z;
}
#Override
public String toString() {
return "Vector, <x = " + x + ", y = " + y + ", z = " + z + ">";
}
public static void main(String[] args) {
Vector a = new Vector(1, 0, 0);
Vector b = new Vector(0, 1, 0);
Vector c = a;
int x = 1;
int y = 2;
int z = 3;
a.add(b);
b.add(b);
c.add(c);
c.silly(x, y, z);
System.out.println("a: " + a);
System.out.println("b: " + b);
System.out.println("c: " + c);
System.out.println("x: " + x + "\ty: " + y + "\tz: " + z);
}
}
I have obviously been unclear in my question, sorry about that. I got this as practice from my teacher and I am supposed to explain the output of the last 4 lines in the code. I have no idea why the output looks as it does. I'm not very good at alias and so on. Someone might be able to give me an explanation? Thanks.
Vector c = a;
means that you create reference which is linked to reference a and its object. You don't call a constructor there. You don't create any object there. Just new reference
The only question I can see is 'What is the relation between Vectors a and c?' So I'll answer that.
When you use the 'new' keyword you are creating a new object which is stored in the heap. So 'a' and 'b' are two separate objects when they have been instantiated. When you say:
Vector c = a;
You are not creating a new object in the heap, merely making a new reference to the same object. So now both 'a' and 'c' are referencing the same thing. If you change a, c will change, and vice versa.
When:
c.add(c);
Is called then the ints in c are simply being added to themselves.
I'm working on a lab for school and I have it almost completed, but there's one part that I can't get to work. The inheritance works except when I get to Cube. For some reason, it won't calculate the Area or Volume (it just comes up with 0). I'm thinking it's a problem with the way I have the inheritance from Square to Cube. Help would be awesome!
package InheritanceTest;
import javax.swing.JOptionPane;
public class InheritanceTest {
public static void main(String[] args) {
String input = "";
Point point = new Point();
input = getinput("Set variable X");
point.setx(input);
input = getinput("Set variable Y");
point.sety(input);
System.out.println("Point, x = " + point.getx() + " y = " + point.gety());
Square square = new Square();
input = getinput("Set variable Side Length");
square.setSideLength(input);
System.out.println("Square, x = " + point.getx() + " y = " + point.gety()
+ " Area = " + square.getAreaOfSquare() + " Perimeter = "
+ square.getPerimeterOfSquare());
Cube cube = new Cube();
input = getinput("Set variable depth");
cube.setDepth(input);
System.out.println("cube, x = " + point.getx() + " y = " + point.gety()
+ " Depth = " + cube.getDepth() + " Area = " + cube.getAreaOfCube()
+ " Volume = " + cube.getVolumeOfCube());
}
private static String getinput(String string) {
String x = JOptionPane.showInputDialog(string);
return x;
}
}
package InheritanceTest;
public class Cube extends Square {
private int depth;
Cube() {
super();
depth = 0;
}
Cube(int x, int y, int sideLength, int d) {
super(x, y, sideLength);
this.depth = d;
}
public int getAreaOfCube() {
return (6 * sideLength * sideLength);
}
public int getVolumeOfCube() {
return (sideLength * sideLength * sideLength);
}
public String getDepth() {
return Integer.toString(depth);
}
public void setDepth(String i) {
depth = Integer.parseInt(i);
}
}
package InheritanceTest;
public class Point {
private int x;
private int y;
Point() {
x = 0;
y = 0;
}
Point(int x, int y) {
this.x = x;
this.y = y;
}
public String getx() {
return Integer.toString(x);
}
public String gety() {
return Integer.toString(y);
}
public void setx(String input) {
x = Integer.parseInt(input);
}
public void sety(String input) {
y = Integer.parseInt(input);
}
}
package InheritanceTest;
public class Square extends Point {
protected int sideLength;
Square() {
super();
sideLength = 0;
}
Square(int x, int y, int l) {
super(x, y);
this.sideLength = l;
}
public int getAreaOfSquare() {
return sideLength * sideLength;
}
public int getPerimeterOfSquare() {
return sideLength + sideLength;
}
public String getSideLength() {
return Integer.toString(sideLength);
}
public void setSideLength(String input) {
sideLength = Integer.parseInt(input);
}
}
When you create cube (new Cube()) you aren't setting the side length (or x and y) for the Square Object it extends.
Cube(){
// This is the constructor called.
super();
depth = 0;
}
Cube(int x, int y, int sideLength, int d){
super(x, y, sideLength);
this.depth = d;
}
You probably want extract the x,y and length values into variables and use "new Cube(x, y, length, depth)"
Something like the following
String x = getinput("Set variable X");
String y = getinput("Set variable Y");
String sideLength = getinput("Set variable Side Length");
String depth getinput("Set variable depth");
Cube cube = new Cube(x, y, sideLength, depth);
Look at how you are defining getVolumeOfCube(). You are calculating volume with sideLength, but you never set sideLength to any non-zero value. Change sideLength to depth and you will get the value you are looking for.
As the title suggests, I'm working on a homework assignment where we are limited to using multi-dimensional arrays in order to create a program that finds two points nearest to each other in a three dimensional space. So far my code looks like this (hybridized from examples in my textbook and my own code):
package exercise7_7;
public class Exercise7_7 {
public static void main(String[] args) {
java.util.Scanner input = new java.util.Scanner(System.in);
System.out.println("Enter the number of points:");
int numberOfPoints = input.nextInt();
double[][] points = new double[numberOfPoints][3];
System.out.println("Enter " + numberOfPoints + " points:");
for (int i = 0; i < points.length; i++) {
points[i][0] = input.nextDouble();
points[i][1] = input.nextDouble();
points[i][2] = input.nextDouble();
}
int p1 = 0, p2 = 1, p3 = 2;
double shortestDistance = distance(points[p1][0] , points[p1][1] , points[p1][2] ,
points[p2][0] , points[p2][1] , points[p2][2]);
for (int i = 0; i < points.length; i++) {
for (int j = i + 1; j < points.length; j++) {
double distance = distance(points[i][0] , points[j][0] , points[j][1] , points[j][2] , points[i][2] , points[j][2]);
if (shortestDistance > distance) {
p1 = i;
p2 = j;
shortestDistance = distance;
}
}
}
System.out.println("The closest two points are " + "(" + points[p1][0] + "," + points[p1][1] +
and (" + points[p2][0] + "," );
}
public static double distance(
double x1, double y1, double z1, double x2, double y2, double z2) {
return Math.sqrt(((x2 - x1) * (x2 - x1)) + ((y2 - y1) * (y2 - y1)) + ((z2 - z1) * (z2 - z1)));
}
}
What I mostly need help with is figuring out just how to get these points compared. I don't think the way I tackled this problem was the best way to do it.
Thanks for the help guys. I'm running on 2 hours of sleep for 2 days now so please excuse any stupid questions or sloppy code.
******
I think I've got it:
package exercise7_7;
public class Exercise7_7 {
public static void main(String[] args) {
java.util.Scanner input = new java.util.Scanner(System.in);
System.out.println("Enter the number of points:");
int numberOfPoints = input.nextInt();
double[][] points = new double[numberOfPoints][3];
System.out.println("Enter " + numberOfPoints + " points:");
for (int i = 0; i < points.length; i++) {
points[i][0] = input.nextDouble();
points[i][1] = input.nextDouble();
points[i][2] = input.nextDouble();
}
int p1 = 0, p2 = 1;
double shortestDistance = distance(points[p1][0] , points[p1][1] , points[p1][2] ,
points[p2][0] , points[p2][1] , points[p2][2]);
for (int i = 0; i < points.length; i++) {
for (int j = i + 1; j < points.length; j++) {
double distance = distance(points[i][0] , points[j][0] , points[j][1] , points[j][2] , points[i][2] , points[j][2]);
if (shortestDistance > distance) {
p1 = i;
p2 = j;
shortestDistance = distance;
}
}
}
System.out.println("The closest two points are " + "(" + points[p1][0] + "," + points[p1][1] + "," + points[p1][2] +
") and (" + points[p2][0] + "," + points[p2][1] + "," + points[p2][2] + ")");
}
public static double distance(
double x1, double y1, double z1, double x2, double y2, double z2) {
return Math.sqrt(((x2 - x1) * (x2 - x1)) + ((y2 - y1) * (y2 - y1)) + ((z2 - z1) * (z2 - z1)));
}
}
Input is taken in, processed, and then outputs the two closest points. Just as a reference, when:
(-1,0,3),(-1,-1,-1),(4,1,1),(2,0.5,9),(3.5,1.5,3),(-1.5,4,2),(5.5,4,-0.5) are inputted, the outcome
seems to be (-1,0,3) and (4,1,1). Could someone confirm that for me.
If this isn't the way to follow up on my own question, I apologize. First day on these slopes and I'm still
learning the ropes.
Use a class to represent your points. This way to you have a distanceTo method that calculates and returns distance. Also you can have a toString method that prints out the point for display to the user. Taking your code rearranging yields this class:
public class ThreeDPoint {
final double x;
final double y;
final double z;
public ThreeDPoint(final double x, final double y, final double z) {
this.x = x;
this.y = y;
this.z = z;
}
public double distanceto(final ThreeDPoint other) {
final double dx = other.x - x;
final double dy = other.y - y;
final double dz = other.z - z;
return Math.sqrt(dx * dx + dy * dy + dz * dz);
}
#Override
public String toString() {
return "{X=" + x + ",Y=" + y + ",Z=" + z + "}";
}
}
Now putting that together gives this, which is much more readable. I have removed the bit where you read points and used random numbers:
public static void main(String args[]) {
final ThreeDPoint[] points = new ThreeDPoint[5];
final Random random = new Random();
for (int i = 0; i < points.length; ++i) {
points[i] = new ThreeDPoint(random.nextInt(100), random.nextInt(100), random.nextInt(100));
}
//store min
double min = Double.POSITIVE_INFINITY;
int first = -1;
int last = -1;
for (int i = 0; i < points.length; ++i) {
for (int j = i + 1; j < points.length; ++j) {
final double d = points[i].distanceto(points[j]);
if (d < min) {
min = d;
first = i;
last = j;
}
}
}
System.out.println("The minimum distance is between point " + first + " and " + last + "(" + points[first] + " and " + points[last] + "). This distance is " + min + ".");
}
private static final class ThreeDPoint {
final double x;
final double y;
final double z;
public ThreeDPoint(final double x, final double y, final double z) {
this.x = x;
this.y = y;
this.z = z;
}
public double distanceto(final ThreeDPoint other) {
final double dx = other.x - x;
final double dy = other.y - y;
final double dz = other.z - z;
return Math.sqrt(dx * dx + dy * dy + dz * dz);
}
#Override
public String toString() {
return "{X=" + x + ",Y=" + y + ",Z=" + z + "}";
}
}