Sev Marc

Greenhorn

Posts: 25

posted 2 years ago

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

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

posted 2 years ago

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");

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");

posted 2 years ago

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!

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

Welcome to the Ranch!

Everything is theoretically impossible, until it is done. ~Robert A. Heinlein

Sev Marc

Greenhorn

Posts: 25

posted 2 years ago

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.

Sev Marc

Greenhorn

Posts: 25

Sev Marc

Greenhorn

Posts: 25

posted 2 years ago

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;

}

}

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;

}

}

Sev Marc

Greenhorn

Posts: 25

posted 2 years ago

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.

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.

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.

Everything is theoretically impossible, until it is done. ~Robert A. Heinlein

Sev Marc

Greenhorn

Posts: 25

posted 2 years ago

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 ...

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

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

Sev Marc

Greenhorn

Posts: 25

posted 2 years ago

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...

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...

posted 2 years ago

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).

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).

Everything is theoretically impossible, until it is done. ~Robert A. Heinlein

Sev Marc

Greenhorn

Posts: 25

Sev Marc

Greenhorn

Posts: 25

Sev Marc

Greenhorn

Posts: 25

Sev Marc

Greenhorn

Posts: 25

Sev Marc

Greenhorn

Posts: 25

posted 2 years ago

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");

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");

posted 2 years ago

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?

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?

Everything is theoretically impossible, until it is done. ~Robert A. Heinlein

Sev Marc

Greenhorn

Posts: 25

posted 2 years ago

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:

addition(input);

break; //showing orphaned case.

case 2:

subtraction(input);

break;

case 3:

multiplication(input);

break;

case 4:

division(input);

break;

}

}

}

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:

addition(input);

break; //showing orphaned case.

case 2:

subtraction(input);

break;

case 3:

multiplication(input);

break;

case 4:

division(input);

break;

}

}

}

posted 2 years ago

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 ;

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 ;

Everything is theoretically impossible, until it is done. ~Robert A. Heinlein

Sev Marc

Greenhorn

Posts: 25

posted 2 years ago

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?

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?

posted 2 years ago

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?

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?

Everything is theoretically impossible, until it is done. ~Robert A. Heinlein

Mark King

Ranch Hand

Posts: 55

Sev Marc

Greenhorn

Posts: 25

posted 2 years ago

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

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.Everything is theoretically impossible, until it is done. ~Robert A. Heinlein

Sev Marc

Greenhorn

Posts: 25

posted 2 years ago

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 .

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 .

Sev Marc

Greenhorn

Posts: 25

Sev Marc

Greenhorn

Posts: 25

Don't get me started about those stupid light bulbs. |