#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    32
    Rep Power
    2

    Calling an overloaded constructor from another class- undefined error


    I tried to make a specific title, but I can't explain it without some extra words.

    So I'm making a program that will be doing arithmetic with fractions.

    The user will input something like 12/3 + 2/5.


    I have two classes. My Fraction class and my FractionApp class.

    Fraction:

    Code:
    import java.util.Scanner;
    
    
    public class Fraction {
    	public Fraction(int n, int d)
    	// #1) create Fraction object with passed in num, denom
    	{
    		Fraction f1 = new Fraction (n,d);
    	}
    	
    	public Fraction ()
    	// #2) create Fraction object with num =1, denom=1
    	{
    		Fraction f1 = new Fraction(1,1);
    	}
    	
    	public Fraction(int n) 
    	// #3) create Fraction object w/passed in num, denom=1
    	{
    		Fraction f1 = new Fraction(n,1);
    	}
    	
    	public String toString()
    	// used to display fraction in format num/denom (ex: 2/3)
    	{
    		String s = toString();
    		return (s);
    	}
    	
    	public boolean equals (Fraction f) 
    	// true if the num/denum of 2 reduced fractions are equal
    	{
    		Fraction f1 = new Fraction(1,1);
    		return (f.equals(f1));
    	}
    	
    	public Fraction plus(Fraction f) 
    	{
    	// adds 2 fractions and returns reduced fraction result
    		
    		Fraction f3 = new Fraction();
    		return(f3);
    	}
    	
    	public Fraction minus(Fraction f)
    	{
    		// subtracts 2 fractions / returns reduced fraction result
    		Fraction f3 = new Fraction();
    		return(f3);
    	}
    	
    	public Fraction times(Fraction f) 
    	// multiplies 2 fractions / returns reduced fraction result
    	{
    		Fraction f3 = new Fraction();
    		return(f3);
    	}
    	
    	public Fraction divide(Fraction f)
    	 // divides 2 fractions / returns reduced fraction result
    	{
    		
    		
    		
    		Fraction f3 = new Fraction();
    		return(f3);
    	}
    	
    	private void reduce ( )
    	// modifies num/denom so fraction reduced to lowest terms
    	{
    		
    	}
    
    	
    }


    FractionApp:
    Code:
    import java.util.*;
    
    public class FractionApp 
    {
    	public static void main (String args[]) 
    	{
    		Scanner s = new Scanner(System.in);
    		
    		Fraction f1 = new Fraction();
    		System.out.println("Enter the expression (like 2/3 + 2/4): ");
    		String first = s.nextLine();
    		
    		String[] spaces = first.split(" ");
    		String[] firstFraction = spaces[0].split("/");
    		String[] secondFraction = spaces[2].split("/");
    		
    		int num1 = Integer.parseInt(firstFraction[0]); 
    		int num2 = Integer.parseInt(secondFraction[0]); 
    		int denom1 = Integer.parseInt(firstFraction[1]);
    		int denom2 = Integer.parseInt(secondFraction[1]); 
    		
    		f1 = Fraction(num1,denom1);
    		Fraction f2 = new Fraction(num2, denom2);
    		
    		
    		if (spaces[1].equals("+")) 
    		{
    			
    		}
    		
    		if (spaces[1].equals("-")) 
    		{
    			
    		}
    		
    		if (spaces[1].equals("*")) 
    		{
    			
    		}
    		
    		if (spaces[1].equals("/")) 
    		{
    			
    		}
    		
    		/*
    		 * 
    		 */
    	}
    }
    However, it's telling me that I can't use "f1 = Fraction(num1,denom1);"
    (in FractionApp)
    Because it's undefined for FractionApp. So how do I call the method properly then?
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    32
    Rep Power
    2
    Yeah, I just had to put a "new" in front of it.
    I'm dumb.
    Feel free to delete this thread, mods.

IMN logo majestic logo threadwatch logo seochat tools logo