Results 1 to 7 of 7
 07292009, 09:30 PM #1Member
 Join Date
 Jul 2009
 Location
 Reading, Pennsylvania
 Posts
 6
 Rep Power
 0
Creating a Defined Class with Member Functions
I am currently trying to create a Class that involves fractions. The concept is very simple: input the fractions and perform various tasks such as displaying the numerator/denominator, and add, subtract, multiply and divide the fractions via methods (or functions). My knowledge about creating methods and classes in java and programming in general is very limited. And was wondering if anyone could help me out.
Here's the code to the methods for my class Fraction:
//Fraction Project Methods
package Fraction_Project;
public class Fraction_Project_Methods {
//Instance variables
private int numerator1;
private int numerator2;
private int denominator1;
private int denominator2;
private int fraction1;
private int fraction2;
private int newNumerator;
private int newDenominator;
//Constructor method, the purpose of this is to initialize instance variables
public Fraction_Project_Methods(){
//Initialize numerator and denominator to zero
numerator1 = 0;
numerator2 = 0;
denominator1= 0;
denominator2 = 0;
fraction1 = 0;
fraction2 = 0;
newNumerator = 0;
newDenominator = 0;
}
public void setFraction(int num1, int denom1, int num2, int denom2){
//set a student's name
numerator1 = num1;
numerator2 = num2;
denominator1 = denom1;
denominator2 = denom2;
fraction1 = (numerator1/denominator1);
fraction2 = (numerator2/denominator2);
}
//Accessor method to obtain the numerator
public int returnNumerator(int i){
//Return the numerator of the fraction
if(i==1){
return numerator1;
}
else{
return numerator2;
}
}
//Accessor method to obtain the denominator
public int returnDenominator(int j){
//Return the denominator of the fraction
if(j==1)
return denominator1;
else
return denominator2;
}
//Methods: add, subtract, multiply, and divide
public int addFractions(){
newNumerator = (numerator1*denominator2)+(numerator2+denominator1 );
newDenominator = (denominator1*denominator2);
return newNumerator/newDenominator;
}
public int subFractions(){
newNumerator = (numerator1*denominator2) (numerator2+denominator1);
newDenominator = (denominator1*denominator2);
return newNumerator/newDenominator;
}
public int divideFractions(){
newNumerator = (numerator1*denominator2);
newDenominator = (denominator1*numerator2);
return newNumerator/newDenominator;
}
public int multiplyFractions(){
newNumerator = (numerator1*numerator2);
newDenominator = (denominator1*denominator2);
return newNumerator/newDenominator;
}
public String toString(){
//Construct and return a string representation of a student
String str;
str = "Fraction one: " + fraction1 + "\n" +
"Fraction two: " + fraction2 + "\n";
return str;
}
}
CODE TO MAIN PROGRAM:
//Fraction Project Main Program
package Fraction_Project;
public class Fraction_Project_Main {
public static void main(String[] args) {
Fraction_Project_Methods f; //Create an object f
f = new Fraction_Project_Methods();
f.setFraction(1, 2, 3, 4);
System.out.println("The first fraction's numerator is: ");
f.returnNumerator(1);
System.out.println("The second fraction's numerator is: ");
f.returnNumerator(2);
System.out.println("The first fraction's denominator is: ");
f.returnDenominator(1);
System.out.println("The second fraction's denominator is: ");
f.returnDenominator(2);
System.out.println("These two fractions added together produces: ");
f.addFractions();
System.out.println("These two fractions subtracted from one another produces: ");
f.subFractions();
System.out.println("These two fractions multipled together produces: ");
f.multiplyFractions();
System.out.println("These two fractions divided produces: ");
f.divideFractions();
System.out.println("This method in string form is:");
f.toString();
}
}
This program currently runs error free, but the methods do not return anything.
Output:
The first fraction's numerator is:
The second fraction's numerator is:
The first fraction's denominator is:
The second fraction's denominator is:
These two fractions added together produces:
These two fractions subtracted from one another produces:
Thanks for your help!
 07292009, 11:32 PM #2Member
 Join Date
 Jul 2009
 Posts
 35
 Rep Power
 0
Simple mistake.
Change
System.out.println("The first fraction's numerator is: ");
f.returnNumerator(1);
to
System.out.println("The first fraction's numerator is: "+f.returnNumerator(1));
Do the same for all the others. It isn't printing anything because its not in the print statement.
 07302009, 12:26 AM #3Member
 Join Date
 Jul 2009
 Location
 Reading, Pennsylvania
 Posts
 6
 Rep Power
 0
ah! ok. thanks! It worked perfectly!
 07302009, 06:18 AM #4
 Join Date
 Jul 2007
 Location
 Colombo, Sri Lanka
 Posts
 11,371
 Blog Entries
 1
 Rep Power
 20
Didn't you notice any errors on that when you run the code? ;)
Anyway, best thing is start from the first step, I mean the first thing you want to do, say addition. Next important thing is the design, take a piece of paper and write down the logic for the process. Implementation is the next step. More important thing is (related object oriented programming) the best way, use different methods for different process. Actually you can learn those on OO studies. Seems to me you've done it in much better way. But still there are few things to improve.
If you have any further questions let us know, but give a try first. ;)
 07302009, 06:52 PM #5Member
 Join Date
 Jul 2009
 Location
 Reading, Pennsylvania
 Posts
 6
 Rep Power
 0
If you are referring to this piece of code:
public int addFractions(){
newNumerator = (numerator1*denominator2)+(numerator2+denominator1 );
I found the mistake. Its supposed to be:
public int addFractions(){
newNumerator = (numerator1*denominator2)+(numerator2*denominator1 );
If not, I would love to know how i could make improvements upon my code!
 08052009, 06:07 AM #6
 Join Date
 Jul 2007
 Location
 Colombo, Sri Lanka
 Posts
 11,371
 Blog Entries
 1
 Rep Power
 20
What type of improvement are you talking about. I've look at your code, and seems to me there is no any improvement you've done. Basically you change some operators there, not means any improvement at all. That's all about the design, or the way you want to find the solution. I couldn't see anything other than that lol.
 08052009, 10:05 PM #7Member
 Join Date
 Aug 2009
 Posts
 2
 Rep Power
 0
Try this...
);)
// This the edited code
// I have added my text where ever i felt necessary
// Although i tried to keep the complete logic and rest of the code same.
// Name of the class is changed :rolleyes:
package Fraction_Project;
public class Fraction_Project_Method {
//Instance variables
private int numerator1;
private int numerator2;
private int denominator1;
private int denominator2;
private double fraction1; // Of Type Double
private double fraction2; // Of Type Double
private double newNumerator; // Of Type Double
private double newDenominator; // Of Type Double
//Constructor method, the purpose of this is to initialize instance variables
public Fraction_Project_Method(){
//Initialize numerator and denominator to zero
numerator1 = 0;
numerator2 = 0;
denominator1= 0;
denominator2 = 0;
fraction1 = 0;
fraction2 = 0;
newNumerator = 0;
newDenominator = 0;
}
public void setFraction(int num1, int denom1, int num2, int denom2){
//set a student's name
numerator1 = num1;
numerator2 = num2;
denominator1 = denom1;
denominator2 = denom2;
fraction1 = (double) numerator1/denominator1; //** if we don't type cast, it'll take the integer value
fraction2 = (double) numerator2/denominator2; //** of fraction and then type cast it. and will become 0 for both
}
//Accessor method to obtain the numerator
public int returnNumerator(int i){
//Return the numerator of the fraction
if(i==1){
return numerator1;
}
else{
return numerator2;
}
}
//Accessor method to obtain the denominator
public int returnDenominator(int j){
//Return the denominator of the fraction
if(j==1)
return denominator1;
else
return denominator2;
}
//Methods: add, subtract, multiply, and divide (I changed the return type to double of ann functions)
public double addFractions(){
newNumerator = (numerator1*denominator2)+(numerator2+denominator1 );
newDenominator = (denominator1*denominator2);
return newNumerator/newDenominator;
}
public double subFractions(){
newNumerator = (numerator1*denominator2) (numerator2+denominator1);
newDenominator = (denominator1*denominator2);
return newNumerator/newDenominator;
}
public double divideFractions(){
newNumerator = (numerator1*denominator2);
newDenominator = (denominator1*numerator2);
return newNumerator/newDenominator;
}
public double multiplyFractions(){
newNumerator = (numerator1*numerator2);
newDenominator = (denominator1*denominator2);
return newNumerator/newDenominator;
}
public String toString(){
//Construct and return a string representation of a student
String str;
str = "Fraction one: " + fraction1 + "\n" +
"Fraction two: " + fraction2 + "\n";
return str;
}
public static void main(String[] args) { //I took main in same class you can take it seperate
Fraction_Project_Method f; //Create an object f
f = new Fraction_Project_Method();
f.setFraction(1, 2, 3, 4);
// You returned the methods but didn't save it in any variable or display...
System.out.println("The first fraction's numerator is: " + f.returnNumerator(1));
System.out.println("The second fraction's numerator is: " + f.returnNumerator(2));
System.out.println("The first fraction's denominator is: " + f.returnDenominator(1));
System.out.println("The second fraction's denominator is: " + f.returnDenominator(2));
System.out.println("These two fractions added together produces: " + f.addFractions());
System.out.println("These two fractions subtracted from one another produces: " + f.subFractions());
System.out.println("These two fractions multipled together produces: " + f.multiplyFractions());
System.out.println("These two fractions divided produces: " + f.divideFractions());
System.out.println("This method in string form is: \n" + f); // to string method is automatically envoked
}
}
Similar Threads

Error tag.getAsString:component context is not defined
By bbq in forum Advanced JavaReplies: 4Last Post: 01052011, 04:00 PM 
How to call defined java class in Jsp
By asheeshiit in forum JavaServer Pages (JSP) and JSTLReplies: 1Last Post: 03042009, 05:21 AM 
have "regular" functions  with no class
By itaipee in forum New To JavaReplies: 2Last Post: 01292009, 04:12 PM 
Tip for System Defined line separator in a Batch File
By Java Tip in forum java.langReplies: 0Last Post: 04042008, 03:48 PM 
Error: Cannot access protected member long getTimeInMillis() in class Calendar
By cachi in forum Advanced JavaReplies: 1Last Post: 08072007, 08:53 AM
Bookmarks