File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes java program using a class fraction Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "java program using a class fraction" Watch "java program using a class fraction" New topic
Author

java program using a class fraction

Jim Harvey
Greenhorn

Joined: Jun 08, 2007
Posts: 7
Okay you experts, I need need help with an assignment:

Rational fractions are of the form a/b, where a and b are integers and b !=0. By "fraction" we mean rational fractions. Suppose a/b and c/d are fractions. Arithmetic operations on fractions are defined by the following rules:

a/b + c/d = (ad + bc)/bd
a/b - c/d = (ad - bc)/bd
a/b * c/d = ac/bd
(a/b) / (c/d) = ad /bc

I have to design the class fraction that can be used to manipulate fractions in a program. The class fraction must include methods to add, subtract, multiply, and divide fractions. When you add, subtract, multiply, or divide fractions, your answer need not be in the lowest terms.

Write a Java console program using the class fraction that performs operations on fractions. Override the method toString so that the fraction can be output using the output statement.

Can someone please help me with this assignment or at least get me started in the right direction. Thanks in advance/jrh
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14074
    
  16

What exactly do you not understand about the assignment? I guess you understand what a fraction is?

Write a class named Fraction that holds the numerator and the denominator and implement methods like add, subtract etc. in the class.

Note, this question does not belong in the "Advanced Java" forum.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Garrett Rowe
Ranch Hand

Joined: Jan 17, 2006
Posts: 1296
Here's a starting point that I hope doesn't give away too much:


[ June 08, 2007: Message edited by: Garrett Rowe ]

Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them. - Laurence J. Peter
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3432
    
  12
should be


Joanne
Garrett Rowe
Ranch Hand

Joined: Jan 17, 2006
Posts: 1296
Originally posted by Joanne Neal:
should be


I understand your point here, but normally I like to override toString with debugging information, so I'll create another method that formats the String for presentation. My toString() method would probably look like this:

But thats just a personal preference, YMMV.
Garrett Rowe
Ranch Hand

Joined: Jan 17, 2006
Posts: 1296
But then I didn't read the requirements very well did I?

Override the method toString so that the fraction can be output using the output statement.
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3432
    
  12
But it's always best to fulfil the original specification
Override the method toString so that the fraction can be output using the output statement.



Posted before I saw Garrett's last post - bit slow on a Friday afternoon
[ June 08, 2007: Message edited by: Joanne Neal ]
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
So, Jim, still with us? We often suggest you start with a very small part of the problem and some test code. Let's try an example here. Can you make this work?

The ranch thrives on code that almost works. Show us what you have at any time and we'll help you figure where to go next.


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Doug Cates
Greenhorn

Joined: Jun 16, 2007
Posts: 7
I am working on this same problem and struggling mightily!! I am about to post what I have, so you are totally allowed to think how stoopid I am, just please don't say it:


It is a work in progress, but I am totally confused. Any help I can get would be very greatly appreciated!!
[ June 17, 2007: Message edited by: Doug Cates ]
Garrett Rowe
Ranch Hand

Joined: Jan 17, 2006
Posts: 1296
Welcome to Javaranch! First of all when posting code please use code tags, they help preserve the formatting of the code and generally make the code easier to read. Secondly when asking questions, always try to tell the details of the specific problem you're having.

On to your specific problems. First, all code in a Java class must be inside a method.* You define a main method in your code:
but it doesn't compile (you need '{}' braces to open and close the body of a concrete method, a ';' is not allowed).

My suggestion would be to start again anew. Don't throw this code away, there's some good stuff in there if used in the right context. But you should start with a much tinier piece. Try coding *just enough* to get the code Stan James posted earlier to print "true", and then come back and show us what you have.




*Unless it is executed within a static initializer
[ June 17, 2007: Message edited by: Garrett Rowe ]
Doug Cates
Greenhorn

Joined: Jun 16, 2007
Posts: 7
I don't understand this part of the code:

Thanks

[ June 17, 2007: Message edited by: Doug Cates ]
[ June 17, 2007: Message edited by: Doug Cates ]
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14074
    
  16

Do you understand the structure of classes and methods?

You create a class, and inside the class you can add methods. The starting point of each Java program is a method called 'main' that has an array of strings as arguments and that returns 'void'. The main method must also be 'public' and 'static'. A class should look like this:

Have a look at The Java Tutorial for more information and lots of examples.
Doug Cates
Greenhorn

Joined: Jun 16, 2007
Posts: 7
Cool, thanks for the explanation, I'll check it out and see you guys later.
Thanks
Jim Harvey
Greenhorn

Joined: Jun 08, 2007
Posts: 7
Here's what I came up with.
*/
public class
{

/**
* @param args
*/
public static void main( String[] args )
{
Scanner input = new Scanner( System.in ); // Used for getting user input

System.out.println( "Fractions should be entered in the following format: a/b " );
System.out.println( "Where 'a' is the numerator, and 'b' is the denominator" );
System.out.println( "" );

// This indicates whether the user should be prompted to enter another operation
boolean askAgain = true;

while( askAgain ) {

try {

// Prompt for the first fraction and create it
System.out.print( "Please enter the first fraction in the format 'a/b': " );
String strFraction1 = input.nextLine();
Fraction fraction1 = new Fraction( strFraction1 );

// Prompt for the second fraction and create it
System.out.print( "\nPlease enter the second fraction in the format 'a/b': " );
String strFraction2 = input.nextLine();
Fraction fraction2 = new Fraction( strFraction2 );

// Prompt for the operation
System.out.println( "Please select one of the following operations: " );
System.out.println( "a. Addition" );
System.out.println( "b. Subtraction" );
System.out.println( "c. Multiplication" );
System.out.println( "d. Division" );
String operation = input.nextLine().trim();

Fraction result = null; // The resulting fraction
String operator = null; // The operator symbol representing this operation
if( operation.equalsIgnoreCase( "a" ) || operation.equalsIgnoreCase( "Addition" )) {
result = fraction1.add( fraction2 );
operator = " + ";
} else if( operation.equalsIgnoreCase( "b" ) || operation.equalsIgnoreCase( "Subtraction" )) {
result = fraction1.subtract( fraction2 );
operator = " - ";
} else if( operation.equalsIgnoreCase( "c" ) || operation.equalsIgnoreCase( "Multiplication" )) {
result = fraction1.multiply( fraction2 );
operator = " * ";
} else if( operation.equalsIgnoreCase( "d" ) || operation.equalsIgnoreCase( "Division" )) {
result = fraction1.divide( fraction2 );
operator = " / ";
}

System.out.println( "\nThe following is the result: " );
System.out.println( fraction1 + operator + fraction2 + " = " + result );
System.out.print( "\nWould you like to perform another operation? (Y/N): " );
String strAskAgain = input.nextLine();
if( strAskAgain.equalsIgnoreCase( "y" )) {
askAgain = true;
} else {
askAgain = false;
}
} catch( Exception e ) {
System.out.println( "\n" + e.getMessage() );
System.out.println( "Please try again" );
askAgain = true;
}

}
}

}
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: java program using a class fraction
 
Similar Threads
Help with error in running program
Don't know where to start???
Need help with Constructors program
sorting fractions problem
null pointer exception