Results 1 to 9 of 9
Like Tree1Likes
  • 1 Post By superhaNds

Thread: Writing 6 Math Methods to Print Results In a Driver From Two Integers

  1. #1
    carlyspade is offline Member
    Join Date
    Jun 2013
    Posts
    17
    Rep Power
    0

    Angry Writing 6 Math Methods to Print Results In a Driver From Two Integers

    The project asks to write a program that prompts the user for two integers and then prints the sum, difference, product, average, max and min. There has to be a class and a separate driver for this. The class has to have a default constructor and a regular constructor that creates and assigns both variables, the 6 methods that compute all the desired math equations, and accessor and mutator methods for each instance field.

    The driver program should prompt the user for two integers, construct a pair object, call the methods of the Pair class to complete the results, and display them using the Scanner class to get user input.

    This is what I have so far for the class:
    Java Code:
     public class Pair
       {
          private int aFirst;
          private int aSecond;
       
          public Pair()
          {
             aFirst =0;
             aSecond =0;
          }
       
          public int getaFirst()
          {
             return aFirst;
          }
       
          public int getaSecond()
          {
             return aSecond;
          }
       
          public void setaFirst (int newFirst)
          {
             aFirst=newFirst;
          }
       
          public void setaSecond (int newSecond)
          {
             aSecond=newSecond;
          }
       
          public static double sum(int aFirst,int aSecond)
          {
             int sum=aFirst+aSecond;
    			return sum;
          }
       
       
       
       
       
       }
    And for the driver:
    Java Code:
    import java.util.Scanner;
    import java.lang.Math;
    
    public class PairTester
    {
    
    public static void main (String [] args)
    {
    int aFirst;
    int aSecond;
    
    Scanner input = new Scanner(System.in);
    
    System.out.print("Enter first number: ");
    aFirst = input.nextInt();
    System.out.print("Enter second number: ");
    aSecond = input.nextInt();
    
    Pair newPair = new Pair(aFirst,aSecond);
    System.out.println(newPair.sum(aFirst,aSecond));
    
    }
    
    
    
    }
    Obviously this is not compiling and I am mostly not understanding how they want us to implement the methods for the math problems within the class itself, I can figure how to do it in the driver but that's not what he wants us to do...thanks in advance for guidance.

  2. #2
    superhaNds is offline Senior Member
    Join Date
    Apr 2013
    Location
    Sweden
    Posts
    232
    Rep Power
    1

    Default Re: Writing 6 Math Methods to Print Results In a Driver From Two Integers

    It is not compiling because you call a constructor that is undefined, you have the default, but you are missing the constructor with parameters, namely, the 2 numbers. Make one
    I think that your sum method is missing the point, you want to manipulate the two fields of the class not other input numbers, so consider making it an instance method that returns num1 + num2
    and follow this philosophy with the rest of the methods. That is how I understood it

  3. #3
    carlyspade is offline Member
    Join Date
    Jun 2013
    Posts
    17
    Rep Power
    0

    Default Re: Writing 6 Math Methods to Print Results In a Driver From Two Integers

    Quote Originally Posted by superhaNds View Post
    It is not compiling because you call a constructor that is undefined, you have the default, but you are missing the constructor with parameters, namely, the 2 numbers. Make one
    I think that your sum method is missing the point, you want to manipulate the two fields of the class not other input numbers, so consider making it an instance method that returns num1 + num2
    and follow this philosophy with the rest of the methods. That is how I understood it
    Java Code:
    public class Pair
       {
          private int aFirst;
          private int aSecond;
       
          public Pair()
          {
             aFirst =0;
             aSecond =0;
          }
    		
    		public Pair(int initialaFirst, int initialaSecond)
    		{
    		aFirst=initialaFirst;
    		aSecond=initialaSecond;
    		}
       
          public int getaFirst()
          {
             return aFirst;
          }
       
          public int getaSecond()
          {
             return aSecond;
          }
       
          public void setaFirst (int newFirst)
          {
             aFirst=newFirst;
          }
       
          public void setaSecond (int newSecond)
          {
             aSecond=newSecond;
          }
       
          public static double expression(int aFirst,int aSecond)
          {
           return aFirst + aSecond;
    So with this static double expression I've officially gotten the driver to display the sum of the two integers:
    Java Code:
    import java.util.Scanner;
    import java.lang.Math;
    
    public class PairTester
    {
    
    public static void main (String [] args)
    {
    int aFirst;
    int aSecond;
    
    Scanner input = new Scanner(System.in);
    
    System.out.print("Enter first number: ");
    aFirst = input.nextInt();
    System.out.print("Enter second number: ");
    aSecond = input.nextInt();
    
    Pair newPair = new Pair(aFirst,aSecond);
    System.out.println(newPair.getaFirst()+newPair.getaSecond());
    Pair newPair1 = new Pair(aFirst,aSecond);
    System.out.println(newPair1.getaFirst()+newPair1.getaSecond());
    
    
    }
    
    
    
    }
    But now I can't make methods for any of the other expressions without it giving me an error how do I implement the rest of the expressions?

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default Re: Writing 6 Math Methods to Print Results In a Driver From Two Integers

    This incomplete method:

    Java Code:
          public static double expression(int aFirst,int aSecond)
          {
           return aFirst + aSecond;
    doesn't make much sense, and even if it were complete, it wouldn't make sense. Why static? Where are your sum, product, divide, and subtract methods?

  5. #5
    carlyspade is offline Member
    Join Date
    Jun 2013
    Posts
    17
    Rep Power
    0

    Default Re: Writing 6 Math Methods to Print Results In a Driver From Two Integers

    Quote Originally Posted by Fubarable View Post
    This incomplete method:

    Java Code:
          public static double expression(int aFirst,int aSecond)
          {
           return aFirst + aSecond;
    doesn't make much sense, and even if it were complete, it wouldn't make sense. Why static? Where are your sum, product, divide, and subtract methods?
    I haven't implemented the other methods yet because I'm honestly not sure what I am supposed to be doing for them I did static because when I was researching on the API it had an expression like that as an example but that was for only ONE expression I couldn't find an example where you are implement 6 different types of expressions as methods.

    This is my first programming class so this is still all pretty foreign to me. I started doing this:
    Java Code:
    public static double add(int aFirst,int aSecond)
          {
           int sum;
    		 sum = aFirst + aSecond;
    		 return sum;
    		 }
    		 
    		 public static double subtract(int aFirst,int aSecond)
    		 {
    		 int difference;
    		 difference = aFirst - aSecond;
    		 return difference;
    Thinking I had something going but my driver still only prints the sum of the two integers that are input by the user. To which now I am lost on. I figured all 6 expressions are supposed to be within one method but I just have no idea how I am supposed to be doing that.

  6. #6
    superhaNds is offline Senior Member
    Join Date
    Apr 2013
    Location
    Sweden
    Posts
    232
    Rep Power
    1

    Default Re: Writing 6 Math Methods to Print Results In a Driver From Two Integers

    the static makes it a class method, it belongs to the class, thus you can call it like CLASSNAME.method
    I think you should have instance methods because you want to manipulate your data fields.

    Java Code:
    public static double expression(int aFirst,int aSecond) {
    return aFirst + aSecond;
    }
    this method computes 2 integers passed in as arguments rather than the class' data fields. It should't be static because it depends on the values of your data fields and why double?
    I was talking about making it like this: public int sum() { return aFirst + aSecond; } this would return the sum of your fields.

    in your system out print you computed the sum by adding the two numbers separately using the get methods
    Fubarable likes this.

  7. #7
    carlyspade is offline Member
    Join Date
    Jun 2013
    Posts
    17
    Rep Power
    0

    Default Re: Writing 6 Math Methods to Print Results In a Driver From Two Integers

    Quote Originally Posted by superhaNds View Post
    the static makes it a class method, it belongs to the class, thus you can call it like CLASSNAME.method
    I think you should have instance methods because you want to manipulate your data fields.

    Java Code:
    public static double expression(int aFirst,int aSecond) {
    return aFirst + aSecond;
    }
    this method computes 2 integers passed in as arguments rather than the class' data fields. It should't be static because it depends on the values of your data fields and why double?
    I was talking about making it like this: public int sum() { return aFirst + aSecond; } this would return the sum of your fields.

    in your system out print you computed the sum by adding the two numbers separately using the get methods
    Ok so now I've done the six different methods for the expressions:
    Java Code:
    public int sum()
          {
             return aFirst + aSecond;
          }
       
          public int difference()
          {
             return aFirst - aSecond;
          }
    		
    		public int product()
    		{
    		return aFirst * aSecond;
    		}
    		
    		public double average()
    		{
    		return (aFirst + aSecond)/2.0;
    		}
    		
    		public int max()
    		{
    		return aFirst>aSecond?aFirst:aSecond;
    		}
    		
    		public int min()
    		{
    		return aFirst<aSecond?aFirst:aSecond;
    		}
    But not 100% sure now on how to organize the driver to print out all 6 expressions I mean it says I am supposed construct a Pair object and then call all methods of the Pair class to compute the results. So I got the Pair object part but as far as calling the methods am I supposed to do something like this? This...doesn't work properly cause it computes 0 but need to know I am on the right track.
    Java Code:
    System.out.print("Enter first number: ");
    aFirst = input.nextInt();
    System.out.print("Enter second number: ");
    aSecond = input.nextInt();
    
    Pair newPair = new Pair();
    int add = newPair.sum();
    System.out.println("The sum is: "+ add );

  8. #8
    superhaNds is offline Senior Member
    Join Date
    Apr 2013
    Location
    Sweden
    Posts
    232
    Rep Power
    1

    Default Re: Writing 6 Math Methods to Print Results In a Driver From Two Integers

    It computes zero because 0 + 0 is zero, you used the default constructor again.
    1. Get the input and pass them to the constructor that takes 2 integers.
    2. Print every result by using all your methods.

    You don't need to store the sum to a variable necessarily, you can say System.out.println("The sum is: "+ newPair.sum());
    Continue to print using the other methods the same way.

  9. #9
    carlyspade is offline Member
    Join Date
    Jun 2013
    Posts
    17
    Rep Power
    0

    Default Re: Writing 6 Math Methods to Print Results In a Driver From Two Integers

    Quote Originally Posted by superhaNds View Post
    It computes zero because 0 + 0 is zero, you used the default constructor again.
    1. Get the input and pass them to the constructor that takes 2 integers.
    2. Print every result by using all your methods.

    You don't need to store the sum to a variable necessarily, you can say System.out.println("The sum is: "+ newPair.sum());
    Continue to print using the other methods the same way.
    WOO finally got it, lol thank you so much sorry that took a little bit to click in my brain.

Similar Threads

  1. How do you print results from a text file in java
    By efceddy in forum New To Java
    Replies: 0
    Last Post: 04-05-2013, 02:22 PM
  2. writing results of recursion into file
    By sara12345 in forum New To Java
    Replies: 2
    Last Post: 04-12-2010, 01:22 PM
  3. Wrong Numeric Results with Right Math Implementation.
    By ‹bermenschen in forum Advanced Java
    Replies: 4
    Last Post: 04-02-2010, 02:52 AM
  4. Writing a MIDI SysEx driver for JSynthLib
    By freqrush in forum Introductions
    Replies: 0
    Last Post: 08-04-2009, 10:46 PM
  5. Using the Math Trig Methods
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-16-2008, 10:54 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •