Skip to Main Content

New to Java

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Fraction class and driver that should multiply, add

843789Apr 29 2009 — edited Apr 30 2009
Please help,

I'm very new to Java and I can't get this code down. I have included coments within the code to explain what is should do.

It should print out like this:

Enter numerator; then denominator.
5
8
5/8

Enter numerator; then denominator.
4
10
4/10
Sum:
82/80
1.025
Product:
20/80
0.25

Enter numerator; then denominator.
6
0
infinity


The first one is the driver class, which I must be missing some information:
public class DudeProg7
{
   public static void main(String[] args)
   {
   Scanner stdIn = new Scanner(System.in);
   Fraction c, d, x; // Fraction objects

   System.out.println("Enter numerator; then denominator.");
   c = new Fraction(stdIn.nextInt(), stdIn.nextInt());
   c.print();

   System.out.println("Enter numerator; then denominator.");
   d = new Fraction(stdIn.nextInt(), stdIn.nextInt());
   d.print();

   x = new Fraction(); // create a fraction for number 0

   System.out.println("Sum:");
   x.add(c).add(d);
   x.print();
   x.printAsDouble();

   x = new Fraction(1, 1); // create a fraction for number 1

   System.out.println("Product:");
   x.multiply(c).multiply(d);
   x.print();
   x.printAsDouble();

   System.out.println("Enter numerator; then denominator.");

   x = new Fraction(stdIn.nextInt(), stdIn.nextInt());
   x.printAsDouble();
   }// end main
Here is the error I get on this:
D:\School\Intro to Programming\DudeProg7.java:39: reached end of file while parsing
}// end main
^
1 error

Tool completed with exit code 1

I know it's a closing bracket somewhere and I bet there are other issues too.

Here is the driver:
/**************************************************
* Fraction class that should do the following:
*
* add - This method receives a Fraction parameter 
* and adds the parameter fraction to the calling object fraction.
*
* multiply - This method receives a Fraction parameter 
* and multiplies the parameter fraction by the calling object fraction.
*
* print - This method prints the fraction using fraction notation (1/4, 21/14, etc.)
*
* printAsDouble - This method prints the fraction as a double (0.25, 1.5, etc.)
*
* Separate accessor methods for each instance variable in the Fraction class. 
****************************************************/

public class Fraction
{
   private int numerator;
   private int denominator;
   
    public Fraction()
	{
		this(0, 1);
	}
    public Fraction(int numerator, int denominator)
	{
		this.numerator = numerator;
		this.denominator = denominator;
	}
    //*******************************************************
    //this method adds fractions.
	public Fraction add(Fraction d)
	{
		numerator = numerator * d.denominator + d.numerator * denominator;
		denominator = d.denominator * denominator;
		return d;
	}
	//*********************************************************
	//This method multiplies fractions.
	public Fraction multiply(Fraction d)
	{
		this.numerator = numerator * d.numerator;
		this.denominator = denominator * d.denominator;
		return d;
	}
	//********************************************************
	//This method prints the fractions in fraction notation.
	public void print()
	{
		System.out.println(numerator + "/" + denominator);
	}
	//********************************************************
	//This method prints the fractions as a double.
	public void printAsDouble()
	{
		System.out.println((double) numerator / (double) denominator);
	}
}
This compiles, but when I run the program I get this:
Exception in thread "main" java.lang.NoSuchMethodError: main
Press any key to continue . . .

Your help is greatly appreciated!

Cheers,
noClueDude
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on May 28 2009
Added on Apr 29 2009
19 comments
364 views