# Probably something simple but stuck

Printable View

• October 4th, 2013, 10:00 PM
JWolf82
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
• October 4th, 2013, 10:33 PM
GoodbyeWorld
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.
• October 5th, 2013, 12:48 AM
JWolf82
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
• October 5th, 2013, 04:03 AM
GregBrannon
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.
• October 5th, 2013, 12:59 PM
JWolf82
Re: Probably something simple but stuck
Thanks Goodbye world! You were exactly right, it's all so obvious now.