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:
• Campbell Ritchie
• Jeanne Boyarsky
• Ron McLeod
• Paul Clapham
• Liutauras Vilda
Sheriffs:
• paul wheaton
• Rob Spoor
• Devaka Cooray
Saloon Keepers:
• Stephan van Hulst
• Tim Holloway
• Carey Brown
• Frits Walraven
• Tim Moores
Bartenders:
• Mikalai Zaikin

Need help with assignment please.

Greenhorn
Posts: 25
• Number of slices to send:
Optional 'thank-you' note:
Hey guys, this is my first post in these forums.. Im a beginner so sorry for any stupid mistakes i've made. Anyways, so I have an assignment due soon and im really stuck and confused on what to do.

The question is:a / b - c / d = (ad - bc) / bd
a / b × c / d = ac / bd
(a / b) / ( c / d) = ad / bc , where c / d ≠ 0

Fractions are compared as follows: a / b op c/ d if ad op bc, where op is any of the relational operations. For example, a / b < c / d if ad < bc.

Design the class Fraction that can be used to manipulate fractions in a program.

Among others, 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. Also, override the method toString so that the fractions can be output using the output statement.

Write a Java program (FractionDemo) with the main method) that, using the class Fraction, performs operations on fractions.

Okay so this is my class so far:

public class Fractions {

private double numerator;
private double denominator;
private int result;

public Fractions() {
numerator = 0;
denominator = 1;
}

public Fractions(int numerator, int denominator) {
this.numerator = numerator;
this.denominator = denominator;
}

public Fractions(int numerator) {
numerator = 0;
}

@Override
public String toString() {
String value = this.getNumerator().getDeno0minator()
+ " numerator = "
+ this.numerator
+ ", "
+ " denominator = "
+ this.denominator;

return value;
}

public double getNumerator() {
return (int) this.numerator;
}

public double getDenominator() {
return (int) this.denominator;
}

public double addition(int a, int b, int c, int d) {
double numerator = (a*d) + (b*c)
double denominator = b*d

return (int) result;
}

public int subtractions(int a, int b, int c, int d) {

double numerator = (a*d) - (b*c);
double denominator = b*d;

return (int) result;
}

public double multiply(int a, int b, int c, int d) {
double numerator = (a*d) * (b*c)
double denominator = b*d

return result;
}

public int division(int a, int b, int c, int d) {

double numerator = (a*d) / (b*c)
double denominator = b*d

return result;
}
}

I will get to the main after we fix the stupid mistakes i've made. Will appreciate all the help I get guys and gals. Thanks alot

Sev Marc
Greenhorn
Posts: 25
• Number of slices to send:
Optional 'thank-you' note:
My main method so far. This is the direction I want to go in

public class FractionsMain{

public static void main(String[] args){

Fractions f1 = new Fractions (a,b);
Fractions f2 = new Fractions (c,d);

Scanner keyboard = new Scanner(System.in);
System.out.println("Enter numerator");
numerator = keyboard.nextDouble();

System.out.println("Enter denominator");
denominator = keyboard.nextInt();

System.out.println("Which of the following would you want to do? * , / , + , -?");

System.out.println("Press a for addition");
System.out.println("Press B for subtraction");
System.out.println("Press c for multiplication?");
System.out.println("Press D for division");

Sev Marc
Greenhorn
Posts: 25
• Number of slices to send:
Optional 'thank-you' note:
Anybody have an idea?

Ranch Hand
Posts: 208
9
• Number of slices to send:
Optional 'thank-you' note:
It might help if you could tell us what confused you.

Oh, and heads up, you can UseCodeTags to make your code easier to read (that's a link).

Welcome to the Ranch!

Sev Marc
Greenhorn
Posts: 25
• Number of slices to send:
Optional 'thank-you' note:
Hey, thanks for the heads up about codetags. Will make sure to use next time. My main issue is that im stuck on that code and I really dont know how to procede after. I dont want you guys to just post a code and say there you go but If you guys could give me hints about which code or what I should write, it would be greatly appreciated.

Tina Smith
Ranch Hand
Posts: 208
9
• Number of slices to send:
Optional 'thank-you' note:
What specifically do you want your code to do that it is not doing right now?

Sev Marc
Greenhorn
Posts: 25
• Number of slices to send:
Optional 'thank-you' note:
Well my issue is the class. I know that there are stuff wrong in it and I was wondering if anybody here could help me fix it.

Sev Marc
Greenhorn
Posts: 25
• Number of slices to send:
Optional 'thank-you' note:
This is my class. The error im getting is at line 26. Its saying double cannot be dereferenced.
public class Fractions {

private double numerator;
private double denominator;
private int result;

public Fractions() {
numerator = 0;
denominator = 1;
}

public Fractions(int numerator, int denominator) {
this.numerator = numerator;
this.denominator = denominator;
}

public Fractions(int numerator) {
numerator = 0;
}

@Override
public String toString() {
String value = this.getNumerator().getDenominator()
+ " numerator = "
+ this.numerator
+ ", "
+ " denominator = "
+ this.denominator;

return value;
}

public double getNumerator() {
return (int) this.numerator;
}

public double getDenominator() {
return (int) this.denominator;
}

public double addition(int a, int b, int c, int d) {
double numerator = (a*d) + (b*c);
double denominator = b*d;

return (int) result;
}

public int subtractions(int a, int b, int c, int d) {

double numerator = (a*d) - (b*c);
double denominator = b*d;

return (int) result;
}

public double multiply(int a, int b, int c, int d) {
double numerator = (a*d) * (b*c);
double denominator = b*d;

return result;
}

public int division(int a, int b, int c, int d) {

double numerator = (a*d) / (b*c);
double denominator = b*d;

return result;
}
}

author
Posts: 23951
142
• Number of slices to send:
Optional 'thank-you' note:

Can you tell us, what this line ...

is supposed to do?

Henry

Sev Marc
Greenhorn
Posts: 25
• Number of slices to send:
Optional 'thank-you' note:
When I asked my colleagues (because I was kind of confused also), they said it will help you get the numerator and denominator (for that specific string part) and that its an essential part of the code.

Tina Smith
Ranch Hand
Posts: 208
9
• Number of slices to send:
Optional 'thank-you' note:
Here's a couple of the obvious errors I see. There is a little code button when you make your posts, if you highlight your code then click it, it will add fancy code tags, as I have done in this post.

String value = this.getNumerator().getDenominator()

this.getNumerator() returns a double. Double doesn't have a getDenominator() method. I'm guessing this is line 26, but without code tags I can't be sure.

Why are you using doubles at all? Have you ever seen a fraction 3.5/7.8908? Integers (int) is a much better type to use because that's how you would represent fractions on pen and paper.

What does the addition() method return? (hint, it's not what you think) The rest of your calculation methods do the same thing.

Sev Marc
Greenhorn
Posts: 25
• Number of slices to send:
Optional 'thank-you' note:
Well I changed all the ints to doubles and Im still getting the same error . Also what do you mean by

"What does the addition() method return? (hint, it's not what you think) "

?

Henry Wong
author
Posts: 23951
142
• Number of slices to send:
Optional 'thank-you' note:

Sev Marc wrote:Well I changed all the ints to doubles and Im still getting the same error .

As Tina already mentioned, your error is caused by the line that she (and I) pointed you to -- and for the reason she described. The other thing (int and doubles) is a different thing -- that likely needs to be address later. And ...

Sev Marc wrote:Also what do you mean by

"What does the addition() method return? (hint, it's not what you think) "

?

Is another thing that likely needs to be addressed later. Perhaps it is a good idea to fix the compile error first -- see Tina's last post (the first part) again.

Henry

Tina Smith
Ranch Hand
Posts: 208
9
• Number of slices to send:
Optional 'thank-you' note:
To put this in different terms?

You can translate this line into two method calls.

See the problem?

Sev Marc
Greenhorn
Posts: 25
• Number of slices to send:
Optional 'thank-you' note:
so should I do this around line 41-44?

public double getDenominator(double denominator) {
return this.denominator;
}

or is it because I did not use this.getDenominator?

Ranch Hand
Posts: 411
5
• Number of slices to send:
Optional 'thank-you' note:
From your posts, I can see that your confusion is a result of not taking adequate time to learn and understand the semantics of the programming constructs...

Not knowing that you cannot call methods on primitives is a bad sign...

Not knowing the narrowing and promotion conversions is another...

If you really want to succeed at learning I suggest you invest some time to fully understand the topic at hand by doing some reading...

Tina Smith
Ranch Hand
Posts: 208
9
• Number of slices to send:
Optional 'thank-you' note:
The second one. If you add in another getDenominator method you will have two methods with the same signature and the code won't compile (because it doesn't know which one to call).
And use this.getDenominator in your toString method, not your getDenominator method or you will get a stack overflow due to something called recursion (a method calling itself, which calls itself, which calls itself ... and so on).

Sev Marc
Greenhorn
Posts: 25
• Number of slices to send:
Optional 'thank-you' note:
Well im most definitely confused but im trying my best also..

@ Tina, So I need to separate the two methods with semi colons? Or do I only use the this.getDenominator

Tina Smith
Ranch Hand
Posts: 208
9
• Number of slices to send:
Optional 'thank-you' note:
Sev,
Let's take a step back. Can you tell me (in english, not code) what your toString() method is supposed to return?

Sev Marc
Greenhorn
Posts: 25
• Number of slices to send:
Optional 'thank-you' note:
Well the toString method is basically used to return a "string" representation of the object. In the question, it says override the method toString so that the fractions can be output using the output statement.

Tina Smith
Ranch Hand
Posts: 208
9
• Number of slices to send:
Optional 'thank-you' note:
Okay, and what do you want your output string to look like?

Sev Marc
Greenhorn
Posts: 25
• Number of slices to send:
Optional 'thank-you' note:
Well, I want it to represent it a fraction.. I need to be able to have a code that will manipulate fractions.

Tina Smith
Ranch Hand
Posts: 208
9
• Number of slices to send:
Optional 'thank-you' note:
We're just talking about toString right now. What does a fraction look like when you type it out?

When you know that, can you write a line of code that will concatenate a string to get the same answer?

Sev Marc
Greenhorn
Posts: 25
• Number of slices to send:
Optional 'thank-you' note:
Well its something like 1/3 or 5/6 lets say..

Sev Marc
Greenhorn
Posts: 25
• Number of slices to send:
Optional 'thank-you' note:
Was I supposed to do this instead? return numerator + "/" + denominator;

Tina Smith
Ranch Hand
Posts: 208
9
• Number of slices to send:
Optional 'thank-you' note:
That would work. Does everything compile now?

Sev Marc
Greenhorn
Posts: 25
• Number of slices to send:
Optional 'thank-you' note:
Yes it does thanks for the help. Sorry if im being a true pain but english is my third language and im kinda new to java. Now, to my main method. This is what Igot so far

public class FractionsMain{

public static void main(String[] args){

Fractions f1 = new Fractions (a,b);
Fractions f2 = new Fractions (c,d);

Scanner keyboard = new Scanner(System.in);
System.out.println("Enter numerator");
numerator = keyboard.nextDouble();

System.out.println("Enter denominator");
denominator = keyboard.nextInt();

System.out.println("Which of the following would you want to do? * , / , + , -?");

System.out.println("Press a for addition");
System.out.println("Press B for subtraction");
System.out.println("Press c for multiplication?");
System.out.println("Press D for division");

Tina Smith
Ranch Hand
Posts: 208
9
• Number of slices to send:
Optional 'thank-you' note:
One of the most important things to learn when asking for help (especially on an online forum) is how to explain your problem. Just posting code and saying "this is what I have" doesn't help me, because I don't know what you need. My immediate reaction to that code is
1. "that's nice"
2. "you're missing the closing braces on your class"
3. "no code tags"

To make things easier for me (and also for you), clearly explain what you have, and how it is different from what you need in english (i.e. not code). If you have tried any solutions, state those too. This does two things.
1. It takes the guesswork out on my end.
2. It makes you think about your problem, and makes sure that you can explain what that problem is.

By the way I think your english is excellent.

My guess for your problem right now is that it prints out the options for what to do, prompts and accepts input for numerator and denominator. Then prompts for the operation, but pressing a, b, c, or d does nothing?

Sev Marc
Greenhorn
Posts: 25
• Number of slices to send:
Optional 'thank-you' note:
My new issue is that its showing an orphaned case here.

System.out.println("Which of the following would you want to do? * , / , + , -?");

System.out.println("Press a for addition");
System.out.println("Press B for subtraction");
System.out.println("Press c for multiplication?");
System.out.println("Press D for division");

String input = keyboard.nextDouble();

Switch (input);
{
case 1:
break; //showing orphaned case.

case 2:
subtraction(input);
break;

case 3:
multiplication(input);
break;

case 4:
division(input);
break;

}
}
}

Tina Smith
Ranch Hand
Posts: 208
9
• Number of slices to send:
Optional 'thank-you' note:
The compiler is probably concerned that you are missing the default case.
http://docs.oracle.com/javase/tutorial/java/nutsandbolts/switch.html

You may want to add a println statement to your default case telling you what the value that was entered is. (because I guarantee it will be hit)

I recommend you do not use nextDouble() to read input from the keyboard for a character. The letter "a" is not a decimal (double) number such as 3.14. You should use a method that reads either a character or a string.

Oh, and "switch" is lowercase, and does not end with a ;

Sev Marc
Greenhorn
Posts: 25
• Number of slices to send:
Optional 'thank-you' note:
Okay, I was able to fix the switch statement and now to the major issues ( all I did was take off semi colon and make switch lowercase )and probably the last final issues of my main code: Im getting two major problems.

First of all, Its showing That int cannot be converted to String in all of the Cases in the switch statement. Not sure about this how I could fix.

Second of all, Up there I did Fractions (name of my class) f1 = new Fractions (a,b) and the other one. But it says that it cannot find symbols.

I suppose for that, I must of missed a method call or something?

Tina Smith
Ranch Hand
Posts: 208
9
• Number of slices to send:
Optional 'thank-you' note:
First case: you're comparing a string value with an int value. (input is typed as string, and the values in your switch are typed as ints) For all the compiler knows you could be comparing "hello" with 1. It has no idea how to convert "hello" to an int.
What you can do to get it working, assuming you are compiling in Java 7 or later, is to put your "1", "2" etc in quotes to force the compiler to compare strings to strings.

Second case: Where are variables a, b, c, and d declared and initialized?

Ranch Hand
Posts: 55
• Number of slices to send:
Optional 'thank-you' note:
The problem in the switch is that input is of type String and the cases are of type int, to solve this, convert input to int.

Sev Marc
Greenhorn
Posts: 25
• Number of slices to send:
Optional 'thank-you' note:
For first case, where do i exactly put them?

for second case, Arent they initialized in the class package already?
Anyways, this is what I did
Fractions f1 = new Fractions (int a ; int b); showing class expected and ; expect for both.
Fractions f2 = new Fractions (int c ; int d);

Tina Smith
Ranch Hand
Posts: 208
9
• Number of slices to send:
Optional 'thank-you' note:
First case: Scroll to the bottom of this page for an example of switch with strings. http://docs.oracle.com/javase/tutorial/java/nutsandbolts/switch.html

Second case: Not sure what you mean by initialized in the class package. Inside FractionsMain.java this is the first place I see those variables.
You cannot declare variables when calling a method. They have to be declared on a separate line and initialized to a meaningful value before they are used.

Sev Marc
Greenhorn
Posts: 25
• Number of slices to send:
Optional 'thank-you' note:
All my errors are on line 16 and 17 now. I tried this but to no avail (not working)

int a = 0;
int b = 0;
int c = 0;
int d = 0;
Fractions f1 = new Fractions ( int a; int b);
Fractions f2 = new Fractions ( int c; int d);

its showing 'class' expected and that semi colons are expected .

Tina Smith
Ranch Hand
Posts: 208
9
• Number of slices to send:
Optional 'thank-you' note:
You were creating the object correctly before. Put those lines back to the way they were. (but keep the variable declarations)

Sev Marc
Greenhorn
Posts: 25
• Number of slices to send:
Optional 'thank-you' note:
Okay, I did that but I keep getting the error message

Sev Marc
Greenhorn
Posts: 25
• Number of slices to send:
Optional 'thank-you' note:
Okay, I did that but I keep getting the error message Anyway to fix it?. I reallt have no clue what to do.

Tina Smith
Ranch Hand
Posts: 208
9
• Number of slices to send:
Optional 'thank-you' note:
What is the exact text of the error message you're getting? (and the relevant lines of code)

 Consider Paul's rocket mass heater.