• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

java program using a class fraction

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here's a starting point that I hope doesn't give away too much:


[ June 08, 2007: Message edited by: Garrett Rowe ]
 
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
should be
 
Garrett Rowe
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 1296
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1296
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Cool, thanks for the explanation, I'll check it out and see you guys later.
Thanks
 
Jim Harvey
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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;
}

}
}

}
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic