Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Probably something simple but stuck

  1. #1
    Junior Member
    Join Date
    Oct 2013
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Probably something simple but stuck

    Hi I'm very new to Java and am currently in a course learning it. I've created the program below, it is an exercise where the user can input two fractions with whole numbers and they will be add together. My final step is to create a method that will add the two inputs into a proper fraction however when I go to start creating the method I cannot for the life of me figure out how to start calling the individual numbers from the two inputs. It seems like it should be really obvious but I'm at a loss. Any help would be greatly appreciated.import java.util.Scanner;

    //Begin Code

    class Mix_JW extends Fraction_JW{
    public Mix_JW(int n, int m) {super(n,m); }
    public String displayMix() {
    String str="";
    if (first < second) str=first+"/"+second;
    else str= first/second +" "+ first%second+"/"+second;
    return str;
    }//display

    public static String get (){
    Scanner scan = new Scanner (System.in);
    System.out.print("Please enter whole number and fraction as such 1 2/3:");
    String userInput = scan.nextLine();
    userInput =userInput.trim();
    System.out.println("Input is: "+userInput);
    return (userInput);
    } //get

    public static String get2 (){
    Scanner scan = new Scanner (System.in);
    System.out.print("Please enter whole number and fraction as such 1 2/3:");
    String userInput2 = scan.nextLine();
    userInput2 =userInput2.trim();
    System.out.println("Input is: "+userInput2);
    return (userInput2);
    }//get2

    public static int[] parse (String userInput){
    int pos = userInput.indexOf(" ");
    String sNum=userInput.substring(0,pos);
    int iNum = Integer.parseInt(sNum);//first integer
    String sNum2=userInput.substring(pos+1);
    pos= sNum2.indexOf("/");
    String sTop=sNum2.substring(0,pos);
    int iTop = Integer.parseInt(sTop);//second integer
    String sBot=sNum2.substring(pos+1);
    int iBot = Integer.parseInt(sBot);//third integer
    System.out.println("Triple =: "+iNum+"|"+iTop+"|"+iBot);
    int[] sA = {iNum,iTop,iBot};
    return (sA);

    } //parse

    public static void main(String[] args) {
    String userInput = Mix_JW.get();
    int[] iA= parse(userInput);
    Mix_JW first=new Mix_JW(iA[0]*iA[2]+iA[1],iA[2]);
    System.out.println("First Measurement= "+first.displayMix());

    String userInput2 = Mix_JW.get2();
    int[] iB= parse(userInput2);
    Mix_JW second=new Mix_JW(iB[0]*iB[2]+iB[1],iB[2]);
    System.out.println("Second Measurement= "+second.displayMix());
    }

    //main
    }//class

    //End code


  2. #2
    Member GoodbyeWorld's Avatar
    Join Date
    Jul 2012
    Location
    Hidden command post deep within the bowels of a hidden bunker somewhere under a nondescrip building
    Posts
    161
    My Mood
    Stressed
    Thanks
    14
    Thanked 25 Times in 25 Posts

    Default Re: Probably something simple but stuck

    What is "first" and "second"?

    I don't know what code you already have in Fraction_JW.

    Is there any method to get to the variables in the first two inputs in your class Fraction_JN?


    You could either use accessor methods in your superclass getFirst() and getSecond() and have them set in your super class.

    I think to answer your question I will need to see the code in the class Fraction_JW to be certain of what to say.

    The reason I need to see your superclass is because likely the issue should be addressed there.

    What you should do is have class variables in your Fraction_JW class called first and second (I'm assuming you already do because I don't see them created in your Mix_JW class.)
    be set by your constructor and then access them with getFirst() and getSecond() methods that return the variables and put these accessor method in your Fraction_JW class and then call them in the Mix_JW class.

    Or you could make them protected, meaning your subclass could see them, and have your constructor of your superclass set the class variables.

  3. The Following User Says Thank You to GoodbyeWorld For This Useful Post:

    JWolf82 (October 5th, 2013)

  4. #3
    Junior Member
    Join Date
    Oct 2013
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Probably something simple but stuck

    I'm giving it a go with what you suggested. Here is Fraction_JW followed by Pair_JW (the last exercise was a look at extending programs so we have a few layers).

    public class Fraction_JW extends Pair_JW{
    //attributes: NONE
    public Fraction_JW(int a, int b) {
    super(a, b);
    int g=gcd(a,b);
    first = first/g;
    second=second/g;
    }//Fraction_JW
    public void display2() {
    System.out.print(first);
    System.out.print("/");
    System.out.println(second);
    }//display
    public Fraction_JW add (Fraction_JW f1, Fraction_JW f2){
    int gtop=f1.first * f2.second
    + f2.first * f1.second;
    int gbottom= f1.second * f2.second;
    return (new Fraction_JW(gtop,gbottom));
    }
    public static int gcd (int a, int b){
    while (a!=b) {
    if (a>b) a=a-b;
    else b=b-a;
    }//while
    return (a);
    } //gcd
    } //class

    //End Fraction


    ------------------


    //Begin Pair
    import java.util.Scanner;


    class Pair_JW {
    //attributes
    int first;
    int second;

    //object builder
    public Pair_JW(int a, int b) {
    first=a;
    second=b;
    }//Pair

    //tasks
    public void display() {
    //pseudo_code is here
    System.out.print("First integer="+first);
    System.out.println();
    System.out.print("Second integer="+second);
    }//display

    public static void main(String[] args) {
    //pseudo-code is here
    Pair_JW f=new Pair_JW(2, 4);
    f.display();
    }//main
    }//Pair

  5. #4
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    4,529
    My Mood
    Mellow
    Thanks
    157
    Thanked 615 Times in 602 Posts

    Default Re: Probably something simple but stuck

    Please read the Announcement topic that is included at the top of every sub-forum to learn how to post code.

  6. #5
    Junior Member
    Join Date
    Oct 2013
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Probably something simple but stuck

    Thanks Goodbye world! You were exactly right, it's all so obvious now.

Similar Threads

  1. completely stuck on a simple code
    By disp in forum Loops & Control Statements
    Replies: 6
    Last Post: June 30th, 2011, 12:02 PM
  2. Help!, newbie, interested in Java, Very simple code, completely stuck!
    By munkybunky in forum What's Wrong With My Code?
    Replies: 4
    Last Post: June 30th, 2011, 07:25 AM
  3. Stuck again
    By Tate in forum What's Wrong With My Code?
    Replies: 2
    Last Post: November 28th, 2010, 10:22 AM
  4. [SOLVED] My head Hurts... such a simple problem but I'm stuck!
    By Kassino in forum What's Wrong With My Code?
    Replies: 4
    Last Post: May 10th, 2010, 08:38 AM
  5. Problem while programming a simple game of Car moving on a road
    By rojroj in forum Java Theory & Questions
    Replies: 3
    Last Post: April 2nd, 2009, 10:24 AM