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 8 of 8

Thread: Constructors that play at the same time.... ?

  1. #1
    Member MrLowBot's Avatar
    Join Date
    Nov 2018
    Location
    Sweden
    Posts
    130
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Default Constructors that play at the same time.... ?

    Hey.

    This problem might be simple but i seem to mess it up anyways.
    I have two classes, one main and one fraction class.
    They are ment to do different things to fraction.

    Main:
    public class ourM {
     
    	public static void main(String[] args) {
     
    		fraction f1 = new fraction(3, 4);
    		f1.toString();
    		fraction f2 = new fraction(4, 6);
     
    	}
     
    }

    Fraction:
    public class fraction {
     
    	private int t;
    	private int n;
     
    	public fraction(int t, int n) {
    		this.t = t;
    		this.n = n;
    		System.out.println("The fraction is : " + this);
    	}
     
    	public String toString() {
    		return new String(t + "/" + n);
    	}
     
    	public int getNumertor(int n) {
    		this.n = n;
    		return n;
    	}
     
    	public int getDenominator(int t) {
    		this.t = t;
    		return t;
    	}
     
    	boolean isNegative() {
    		if ((getDenominator(t) / getNumertor(n)) < 0) {
    			return true;
    		}
    		return false;
    	}
     
    }

    But when i do something such (toString) i get it twice when i really only want it once because
    i only called it once... so i am guessing that there is something that i am missing.
    "Tick, tack"

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Constructors that play at the same time.... ?

    i get it twice
    Can you post the output that shows what you are asking about?
    Add some comments pointing out the problem and describe what the desired output is.
       f1.toString();
    That statement is useless since it doesn't save or print the String that is returned.
    Why is it there?
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member MrLowBot's Avatar
    Join Date
    Nov 2018
    Location
    Sweden
    Posts
    130
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Default Re: Constructors that play at the same time.... ?

    Quote Originally Posted by Norm View Post
    Can you post the output that shows what you are asking about?
    Add some comments pointing out the problem and describe what the desired output is.
       f1.toString();
    That statement is useless since it doesn't save or print the String that is returned.
    Why is it there?

    That f1.toString does print... but it is just that the code deos it twice.
    Thought that when putting other values in the same constructor and with a different call
    name then it would only use those numbers.

    Will send everything with what i get and what i want to get.
    "Tick, tack"

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Constructors that play at the same time.... ?

    f1.toString does print
    Where? The posted code does not use the value returned by that call to the toString method.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Member MrLowBot's Avatar
    Join Date
    Nov 2018
    Location
    Sweden
    Posts
    130
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Default Re: Constructors that play at the same time.... ?

    Quote Originally Posted by Norm View Post
    Where? The posted code does not use the value returned by that call to the toString method.
    The code that i posted was just a test code that i made up real quick.
    This is the actual assignment.

    And here i am get messed up. I can do all the operations but i don't know of how to use more than one fraction without using more than one class...
    The teachers said that there would only be a need for a main and another class with many methods that got their values from the constructor.
    Aka the constructor should be able to have

    "Fraction f1 = new Fraction;"
    "Fraction f2 = new Fraction;"
    "Fraction f3 = new Fraction;"

    And then do, as an example:
    "f1.toString"

    Exercise 7
    Create a class Fraction.java, representing a fractional number of the form N/D, where N (the numerator) and D (the denominator) both are integers. If the denominator is equal to zero, an error message should be printed. The class should include the following members:

    A constructor, creating and initializing a new fractional number.
    Methods getNumerator and getDenominator, returning the numerator or denominator, respectively.
    Method isNegative, returning true if the fractional number is negative.
    Methods add, subtract, multiply, divide, performing the corresponding operations on two fractional numbers and returning a new fractional number. It is up to you to decide a proper way of handling the case when one of the fractional numbers have a zero denominator.
    isEqualTo, comparing two Fraction-instances, checking if they correspond to the same fractional number.
    toString, returning a string representation of the fractional number on the form N/D.

    Feel free to add more methods, if you think anything is missing. Suitable argument and return types are up to you to decide.
    "Tick, tack"

  6. #6
    Member
    Join Date
    Sep 2018
    Location
    Virginia
    Posts
    284
    My Mood
    Cool
    Thanks
    0
    Thanked 38 Times in 36 Posts

    Default Re: Constructors that play at the same time.... ?

    Your initial classes were mostly correct. But f1.toString() does not print anything. It just returns a String which you failed to capture as a return value.

    Regards,
    Jim

  7. #7
    Member MrLowBot's Avatar
    Join Date
    Nov 2018
    Location
    Sweden
    Posts
    130
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Default Re: Constructors that play at the same time.... ?

    Quote Originally Posted by jim829 View Post
    Your initial classes were mostly correct. But f1.toString() does not print anything. It just returns a String which you failed to capture as a return value.

    Regards,
    Jim
    So.. in my case what would that return value be?
    "Tick, tack"

  8. #8
    Member
    Join Date
    Sep 2018
    Location
    Virginia
    Posts
    284
    My Mood
    Cool
    Thanks
    0
    Thanked 38 Times in 36 Posts

    Default Re: Constructors that play at the same time.... ?

    The string. Look at your toString method. There are two hints. The first is the return type as specified by the method signature. The second is the actual value that is returned with the return statement.

    Regards
    Jim

Similar Threads

  1. constructors
    By chunkymonkey in forum What's Wrong With My Code?
    Replies: 4
    Last Post: September 25th, 2018, 02:19 PM
  2. constructors
    By sumitroy in forum What's Wrong With My Code?
    Replies: 2
    Last Post: June 22nd, 2014, 08:44 AM
  3. Using Date() to get Start Time and Finish Time of a copyFiles method
    By dalythe in forum File I/O & Other I/O Streams
    Replies: 3
    Last Post: June 17th, 2013, 09:50 PM
  4. Constructors
    By av8 in forum What's Wrong With My Code?
    Replies: 6
    Last Post: July 19th, 2011, 06:40 PM
  5. [SOLVED] Overloading constructors(Multiple Constructors)
    By chronoz13 in forum Java Theory & Questions
    Replies: 2
    Last Post: May 11th, 2011, 12:55 PM