• Post Reply Bookmark Topic Watch Topic
  • New Topic

Need help for JGrasp homework exercise  RSS feed

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Our assignment is to compile and execute a Java application program that calculates one dimensional distance between two user-input numbers. We are given two numbers a and b to calculate absolute value of b-a

I created the program, but it keeps giving me compiling errors. This is only our first lab so I'm still not able to find mistakes in my own programs.

public class DimensDistance
{
public static void main (String [] args)
{
double a, b;
double abs;

a = Input.readDouble ("Enter the first number:");
b = Input.readDouble ("Enter the second number:");

abs = b - a;
Math.abs(a - b);


Output.showValue("The first number is", b);
Output.showValue("The second number is", a);
Output.showValue("The absolute value is", abs);

} // method main

} //class DimensDistance
 
Ranch Hand
Posts: 250
1
Chrome Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The errors are no doubt occurring where you use Input.readDouble and Output.showValue. Is this all of your code, because the Java SDK doesn't include any classes named Input or Output.
 
Sammy Wolf
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yea that's all the code I have so far. I don't know how or what I need to change it to make it work
 
Joel Christophel
Ranch Hand
Posts: 250
1
Chrome Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sammy Wolf wrote:yea that's all the code I have so far. I don't know how or what I need to change it to make it work


Have you learned yet how to receive input from keyboard? What makes you think you can do Input.readDouble?
 
Sammy Wolf
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Professor used it in his example, which was

public class Sum2
{
public static void main (String [] args)
{
double x, y;
double sum;

x = Input.readDouble("Enter the first number:");
y = Input.readDouble("Enter the second number:");

sum = x + y;

Output.showValue("The first number is ", x);
Output.showValue("The second number is ", y);
Output.showValue("The sum is ", sum);

} // method main

} // class Sum2
 
Joel Christophel
Ranch Hand
Posts: 250
1
Chrome Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sammy Wolf wrote:Professor used it in his example, which was

public class Sum2
{
public static void main (String [] args)
{
double x, y;
double sum;

x = Input.readDouble("Enter the first number:");
y = Input.readDouble("Enter the second number:");

sum = x + y;

Output.showValue("The first number is ", x);
Output.showValue("The second number is ", y);
Output.showValue("The sum is ", sum);

} // method main

} // class Sum2


Well the above isn't compilable Java code. Generally, we use a Scanner object in Java to receive input from the keyboard like this:

 
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

Have you been supplied with the Input class? If so, copy it into the same directory as your Sum2 class. If not, find my KeyboardInput class and rename it Input. As you will see here, it would be better to call it Inputs. You will have to rename the constructor as well as the class, and remember the name of the file it is in has to be the same as the name of the class. You may have to alter the methodsAnd to create a readDouble method, change all references to int/Int to double/Double.
If you have the Output class, similar procedure. If you don't have Output, change all references to Output to this sort of thing: System.out.println("First number was " + x);
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!