Results 1 to 7 of 7
  1. #1
    New2Java is offline Member
    Join Date
    Jul 2009
    Location
    Reading, Pennsylvania
    Posts
    6
    Rep Power
    0

    Question 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!

  2. #2
    adz
    adz is offline Member
    Join Date
    Jul 2009
    Posts
    35
    Rep Power
    0

    Default

    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.

  3. #3
    New2Java is offline Member
    Join Date
    Jul 2009
    Location
    Reading, Pennsylvania
    Posts
    6
    Rep Power
    0

    Default

    ah! ok. thanks! It worked perfectly!

  4. #4
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    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. ;)

  5. #5
    New2Java is offline Member
    Join Date
    Jul 2009
    Location
    Reading, Pennsylvania
    Posts
    6
    Rep Power
    0

    Default

    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!

  6. #6
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    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.

  7. #7
    varunPandey is offline Member
    Join Date
    Aug 2009
    Posts
    2
    Rep Power
    0

    Default 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

  1. Replies: 4
    Last Post: 01-05-2011, 03:00 PM
  2. How to call defined java class in Jsp
    By asheeshiit in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 03-04-2009, 04:21 AM
  3. have "regular" functions - with no class
    By itaipee in forum New To Java
    Replies: 2
    Last Post: 01-29-2009, 03:12 PM
  4. Replies: 0
    Last Post: 04-04-2008, 02:48 PM
  5. Replies: 1
    Last Post: 08-07-2007, 07:53 AM

Tags for this Thread

Posting Permissions

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