# java program using a class fraction

Jim Harvey

Greenhorn

Posts: 7

posted 8 years ago

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

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

posted 8 years ago

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.

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.

Garrett Rowe

Ranch Hand

Posts: 1296

Garrett Rowe

Ranch Hand

Posts: 1296

posted 8 years ago

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.

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.

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

Garrett Rowe

Ranch Hand

Posts: 1296

posted 8 years ago
Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them. - Laurence J. Peter

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

Posts: 3742

16

Stan James

(instanceof Sidekick)

Ranch Hand

Ranch Hand

Posts: 8791

posted 8 years ago

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.

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

Posts: 7

posted 8 years ago

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 ]

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

Posts: 1296

posted 8 years ago
Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them. - Laurence J. Peter

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 ]

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

Posts: 7

posted 8 years ago

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.

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

Posts: 7

Jim Harvey

Greenhorn

Posts: 7

posted 8 years ago

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;

}

}

}

}

*/

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;

}

}

}

}