• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

multiple errors on my java fraction calc program

 
Richard Scott
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
im trying to get this to work here is the assignment :

Program Chapter 7
Grading Information: This Program is due on date specified in d2l.
Comments are REQUIRED; flow charts and Pseudocode are NOT REQUIRED.
Directions
Points
The files must be called <YourNameProg7.java>. (driver) Fraction.java (handles the fractional numbers)
Example: YourNameProg7.java Fraction.java
Ensure you include ALL files required to make your program compile and run. I would like to see your .java files only.
Proper coding conventions required the first letter of the class start with a capital letter and the first letter of each additional word start with a capital letter.
4
Basic Requirements
Write a driver and fraction class that performs addition, multiplication, prints the fraction, and prints as a double.
YourNameProg7.java
Provide a driver class, YourNameProg7, that demonstrates this Fraction class. The driver class should contain this main method:
public static void main(String[] args) { Scanner stdIn = new Scanner(System.in); Fraction c, d, x; // Fraction objects
System.out.println("Enter numerator; then denominator."); c = new Fraction(stdIn.nextInt(), stdIn.nextInt()); c.print();
System.out.println("Enter numerator; then denominator."); d = new Fraction(stdIn.nextInt(), stdIn.nextInt()); d.print();
x = new Fraction(); // create a fraction for number 0
16
System.out.println("Sum:");
x.add(c).add(d);
x.print();
x.printAsDouble();
x = new Fraction(1, 1); // create a fraction for number 1
System.out.println("Product:");
x.multiply(c).multiply(d);
x.print();
x.printAsDouble();
System.out.println("Enter numerator; then denominator.");
x = new Fraction(stdIn.nextInt(), stdIn.nextInt());
x.printAsDouble();
} // end main
Note that this demonstration driver does not call the accessor methods. That’s OK.
Accessor methods are often implemented regardless of whether there’s an
immediate need for them. That’s because they are very useful methods in general
and providing them means that future code can use them when the need arises.
Fraction.java
Write a Fraction class called Fraction.java that implements these methods:
 add ─ This method receives a Fraction parameter and adds the parameter
fraction to the calling object fraction.
 multiply ─ This method receives a Fraction parameter and multiplies the
parameter fraction by the calling object fraction.
 print ─ This method prints the fraction using fraction notation (1/4, 21/14,
etc.)
 printAsDouble ─ This method prints the fraction as a double (0.25, 1.5,
etc.)
Separate accessor methods for each instance variable in the Fraction class.
56
Upload your page to the Dropbox.
NOTE: Complete your activity and submit it to the Dropbox in d2l
4
Total Points 80
Sample Output
Enter numerator; then denominator. 5 8 5/8
Enter numerator; then denominator. 4 10 4/10 Sum: 82/80 1.025 Product: 20/80 0.25
Enter numerator; then denominator. 6 0 infinity


here is my code this is the driver

and the function.java


any help or advice is greatly appreciated
 
Ulf Dittmer
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What does the code do now, and how is that different from what you expected?
 
Paweł Baczyński
Bartender
Posts: 1818
33
Firefox Browser IntelliJ IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've spotted one error.
Your class is of type Function. Your method add returns Fraction by returning this. You can't do that (because Function is not a Fraction).

Another (potential) error is import Function.java;
Unless you have a class named java in package named Function this is an error. You import classes, not source file names.
And you only need to import a class if it is in a different package.
I dont see any package keyword in your source so I assume that all classes are in default package. So no need to import anything.

There might be more errors. I didn't try to compile your code.
Maybe post error messages from compiler here?
 
Richard Scott
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ulf Dittmer wrote:What does the code do now, and how is that different from what you expected?


they both wont compile

driver

line 1 package function doesn't exist
6 cannot find symbol
7 cannot find symbol
9 cannot find symbol
12 same
14 same

function.java

line 15 cannot find symbol
18 same
22 same
 
Paweł Baczyński
Bartender
Posts: 1818
33
Firefox Browser IntelliJ IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Allow me to focus on Function first.
1. You wrote your source file is function.java yet your class is named Function. They must be named exactly the same + .java extension because java is case sensitive!
2. You got "cannot find symbol" errors because Java compiler can't figure out what Fraction is. You have a class named Function, not Fraction.

And there goes your driver.
1 - package function doesn't exist (well actually javac meant Function). Self-explanatory. I wrote about it in my previous post.
6 - javac don't know what Scanner is. You forgot to import it.
7, 9, 12, 14 - javac don't know what Fraction is. You don't have class named Fraction.

And it would be better if you posted exact error messages. If you don't know how to copy output of the terminal see this (I'm assuming you use Windows).
 
Richard Scott
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pawel Pawlowicz wrote:
Richard Scott wrote:function.java

line 15 cannot find symbol
18 same
22 same


Allow me to focus on Function first.
1. You wrote your source file is function.java yet your class is named Function. They must be named exactly the same + .java extension because java is case sensitive!
2. You got "cannot find symbol" errors because Java compiler can't figure out what Fraction is. You have a class named Function, not Fraction.


so I need to change the name from Function.java to Fraction.java?
 
Paweł Baczyński
Bartender
Posts: 1818
33
Firefox Browser IntelliJ IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Richard Scott wrote:so I need to change the name from Function.java to Fraction.java?

Your class represents a fraction. It would be better if it was named as such. And remember to rename all occurences of Function to Fraction inside your code.

And it looks like you wrote this code without even trying to compile it. Never do that!
Write 3-4 lines od code, compile, check if it works, fix any issues, repeat.

You produced some code, didn't try to compile it while writing and when you finally tried you don;t know where the error is...
 
Richard Scott
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pawel Pawlowicz wrote:
Richard Scott wrote:so I need to change the name from Function.java to Fraction.java?

Your class represents a fraction. It would be better if it was named as such. And remember to rename all occurences of Function to Fraction inside your code.


well that helped
errors for Fraction.java
line 18 cannot find symbol
 
Paweł Baczyński
Bartender
Posts: 1818
33
Firefox Browser IntelliJ IDE Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Richard Scott wrote:errors for Fraction.java
line 18 cannot find symbol
Well... is it a DENOM or is it a DEMON ;)?
You know that javac shows you what symbol it can't find, right? Use that information.

And I already asked you to post exact error messages. Please, copy-paste from command prompt. It would be easier for us to help you .
And it is in your interest to help us help you as much as possible ;).
 
Richard Scott
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pawel Pawlowicz wrote:Allow me to focus on Function first.
1. You wrote your source file is function.java yet your class is named Function. They must be named exactly the same + .java extension because java is case sensitive!
2. You got "cannot find symbol" errors because Java compiler can't figure out what Fraction is. You have a class named Function, not Fraction.

And there goes your driver.
1 - package function doesn't exist (well actually javac meant Function). Self-explanatory. I wrote about it in my previous post.
6 - javac don't know what Scanner is. You forgot to import it.
7, 9, 12, 14 - javac don't know what Fraction is. You don't have class named Fraction.

And it would be better if you posted exact error messages. If you don't know how to copy output of the terminal see this (I'm assuming you use Windows).


here is the errors for the driver Prog7.java:1: error: package Fraction does not exist
import Fraction.java;
^
Prog7.java:15: error: constructor Fraction in class Fraction cannot be applied to given types;
c = new Fraction();
^
required: int,int
found: no arguments
reason: actual and formal argument lists differ in length
.\Fraction.java:18: error: cannot find symbol
demon = denom * fracB.denom;//adds the deniminator
^
symbol: variable demon
location: class Fraction
3 errors
 
Richard Scott
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pawel Pawlowicz wrote:
Richard Scott wrote:errors for Fraction.java
line 18 cannot find symbol
Well... is it a DENOM or is it a DEMON ;)?
You know that javac shows you what symbol it can't find, right? Use that information.

And I already asked you to post exact error messages. Please, copy-paste from command prompt. It would be easier for us to help you .
And it is in your interest to help us help you as much as possible ;).


Got it thanks didn't know that you could do that Fraction.java compiles fine now. but the driver doesn't here is the errors
Prog7.java:1: error: package Fraction does not exist
import Fraction.java;
^
Prog7.java:15: error: constructor Fraction in class Fraction cannot be applied to given types;
c = new Fraction();
^
required: int,int
found: no arguments
reason: actual and formal argument lists differ in length
2 errors
 
Paweł Baczyński
Bartender
Posts: 1818
33
Firefox Browser IntelliJ IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Line 1. You try to import class java from package Fraction (Fraction.java) that does not exist. I already told you about that.
Line 15. You try to invoke no-argument constructor of Fraction class. Your class does not have one thus the error.
 
Richard Scott
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pawel Pawlowicz wrote:Line 1. You try to import class java from package Fraction (Fraction.java) that does not exist. I already told you about that.
Line 15. You try to invoke no-argument constructor of Fraction class. Your class does not have one thus the error.


Do I even need the line 1 import or can I delete it?
How do I solve the line 15 error?
 
Paweł Baczyński
Bartender
Posts: 1818
33
Firefox Browser IntelliJ IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Richard Scott wrote:Do I even need the line 1 import or can I delete it?
How do I solve the line 15 error?

You don't need that import.
And about error at line 15. It was because you tried to use a constructor that don't exist.
So there are two possibilities.
1. Write no-arg constructor
2. Use another constructor that you already have.

I'll leave it up to you to figure out which is better in your case.
 
Richard Scott
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pawel Pawlowicz wrote:
Richard Scott wrote:Do I even need the line 1 import or can I delete it?
How do I solve the line 15 error?

You don't need that import.
And about error at line 15. It was because you tried to use a constructor that don't exist.
So there are two possibilities.
1. Write no-arg constructor
2. Use another constructor that you already have.

I'll leave it up to you to figure out which is better in your case.


ok how so I go about that I am confused.
 
Paweł Baczyński
Bartender
Posts: 1818
33
Firefox Browser IntelliJ IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nope. No-arg constructor is not a good idea. What will your fraction be? 0/0 ?

Or...
3. Don't use any constructor at all.
 
Richard Scott
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pawel Pawlowicz wrote:Nope. No-arg constructor is not a good idea. What will your fraction be? 0/0 ?

Or...
3. Don't use any constructor at all.


here is the sample output this is what I want it to looklike
Sample Output
Enter numerator; then denominator. 5 8 5/8
Enter numerator; then denominator. 4 10 4/10 Sum: 82/80 1.025 Product: 20/80 0.25
Enter numerator; then denominator. 6 0 infinity
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic