Alex R Burgess Alex R Burgess - 1 month ago 8
Java Question

Calculation error in Java

I'm writing a program for an assignment and am getting incorrect calculations when I run the program.

The program I'm creating is designed to take user input to control a robot and then calculate and print out the following:


  • Distance traveled

  • Horizontal position

  • Vertical position

  • Battery Usage



The calculation for battery usage is working fine, but the rest of the calculations print the value 0.0 or -0.0

My code is spread across two classes, one containing the constructor method and all of the calculations, while the other contains the main method with code to take user input and print results.

Class containing the Constructor and all of the Calculations:

class RobotMovement{
private double angle;
private double speed;
private double time;
private double distance;

//Constructor method
public RobotMovement(double a,double s,double t){
angle = a;
speed = s;
time = t;
}

//Set methods
public void setAngle(double a){
angle = a;
}
public void setSpeed(double s){
speed = s;
}
public void setTime(double t){
time = t;
}
public void setDistance(double d){
distance = speed * time;
}

//Get methods
public double getAngle(){
return angle;
}
public double getSpeed(){
return speed;
}
public double getTime(){
return time;
}
public double getDistance(){
return distance;
}

//Calculation Methods
public double calcHorizontal(){
return distance * Math.sin(angle);
}
public double calcVertical(){
return distance * Math.cos(angle);
}
public double calcBattery(){
return time * Math.pow(speed,2) * 3.7;
}
}


Class containing Main method:

import java.util.*;
class RobotUser{
public static void main (String[] args){
Scanner scan = new Scanner(System.in);

//Getting user input for the Robot object
System.out.println("\nPlease enter the Angle, Speed and Time you wish the Robot to travel");

System.out.println("\nAngle:");
double angle = scan.nextDouble();

System.out.println("\nSpeed:");
double speed = scan.nextDouble();

System.out.println("\nTime:");
double time = scan.nextDouble();

//Instantiates RobotMovement
RobotMovement Robot = new RobotMovement(angle,speed,time);

System.out.println("\nThe Robot moved " + Robot.getDistance() + " meters!");

System.out.println("\nThe Robots horizontal position is " + Robot.calcHorizontal());

System.out.println("\nThe Robots vertical position is " + Robot.calcVertical());

System.out.println("\nThe Robot used " + Robot.calcBattery() + " seconds of idle time");



}
}

Answer

I think your issue is that you are never calculating the distance traveled, and in java the default value for the distance variable then becomes 0.0. So when you ask to calculate the answer for the other 3 methods, you're multiplying each answer by 0.0 and that's how you end up with those results. calcBattery is the only one that does not use the distance variable.

TLDR; just calculate the distance before you ask to calculate the other values.

Comments