PDA

View Full Version : Java Help


Epison07
09-12-2005, 07:31 PM
Hello, First I would like to give a little background. I am a sophomore in college and I have C++ under my belt. I am currently taking a Data Structures and Algorithms class, but it is in Java. We have went over the basics in Java, and I figured out it is pretty much alot like C++, but I have this stupid error.
If this is my normal C++ boards, I would probably laugh at myself, becuz my error is "{" missing on like 34. It is pretty simple I would assume, but I cant figure out what im missing. The big thing I have noticed in Java in that everything has to be in classes, so Im not sure if my set up is right. But yeah, Im pretty much tryign to code this program like I would in C++. And yes it is a homework assignment, but Im having syntax problems :-D Please help a Java n00b.

-Ep


> Executing: C:\bin\javac.exe" "C:\dsp1.java"

C:\dsp1.java:34: '{' expected
public class fraction() {
^
1 error
> Execution finished.



import java.util.Scanner;

public class dsp1 {

public static void main(String[] args)
{ fraction x=new fraction(),
y=new fraction(),
result=null;
String op;
Scanner kb=new Scanner(System.in);

System.out.print("please enter an expression int int op int int\n");
System.out.print("for example, 3 4 - 2 3 \n");
System.out.print("where 3 4 and 2 3 represent the fractions 3/4 and 2/3:\n");

x.read(kb); //read the first fraction
op=kb.next(); //read the operator
y.read(kb); //read the second fraction

switch(op.charAt(0))
{ case '+': result=x.plus(y); break;
case '-': result=x.minus(y); break;
case '*': result=x.times(y); break;
case '/': result=x.div(y); break;
default: System.out.println("incorrect operator");
System.exit(1);
}
if (result!=null) System.out.print(x+" "+op+" "+y+" = "+result+"\n");

} //end main()

} //end dsp1

public class fraction() {
public int num, den;

public void read();
public fraction plus(fraction);
/*public fraction minus(fraction);
public fraction times(fraction);
public fraction div(fraction);*/
public void lcf();

void read(){
num = kb.next();
den = kb.next();
return;
}
fraction plus(fraction k){
fraction temp;
temp.den = den * k.den;
temp.num = (num*k.den)+(k.num*den);
temp.lcf();
return temp;
}

void lcf(){
for(int x = den;x > 0;x--){
if(den%x == 0 && num%x == 0){
den = den/x;
num = num/x;
}
}
}
}


Edit: Do I have to define kb for both classes, or just main?

nikkiH
09-12-2005, 08:14 PM
fraction x=new fraction()

Java is case-sensitive. By convention, classes are upper case, so
fraction = new Fraction()
(note that you called the variable the same name as the class: you can't do that, hence my suggestion to use an upper case F)

You also cannot have two classes in the same file.
Java requires each class have its own java file with the same name as the class.
So, Fraction class is in
Fraction.java
and dsp1 class is in
dsp1.java

HTH

KeZZeR
09-13-2005, 12:46 AM
Unless you nest classes ;)

I think you can have 2 classes in the same file as long as you call them from a static reference if i remember correctly?

nikkiH
09-13-2005, 03:03 PM
Yes; that's an inner class (http://java.sun.com/docs/books/tutorial/java/javaOO/innerclasses.html).
Different animal than what was being attempted. Either that or I'm retarded. It's certainly possible. :D

Epison07
09-13-2005, 04:09 PM
fraction x=new fraction()

Java is case-sensitive. By convention, classes are upper case, so
fraction = new Fraction()
(note that you called the variable the same name as the class: you can't do that, hence my suggestion to use an upper case F)

You also cannot have two classes in the same file.
Java requires each class have its own java file with the same name as the class.
So, Fraction class is in
Fraction.java
and dsp1 class is in
dsp1.java

HTH

Ok, let me get this right. You are saying that I called the variable the same name as the class it is, but I am not seeing this.

fraction x=new fraction(),

Wouldnt this be declaring a variable (object) of type fraction. Sorry, but I havent programmed anything for a while, so I am trying to remember C++ and apply it to Java, but in C++. It was like, fraction x; Thats just an object. Then there like, fraction *x = new fraction(); where that would create a pointer and give it a memory address.
Just so you know, my instructor wrote the main class, all we have to write is the fraction class to make the program work.
Please explain what you mean when you said, "note that you called the variable the same name as the class:"
Like I said, I would think the variable name to be x and fraction only to be the indentifer of the data type.

And please dont take my tone as tho I am argueing with you, I get that alot on different boards. Im just trying to understand :-D

-Ep

nikkiH
09-13-2005, 04:13 PM
Oh, dude.
My bad.
I feel retarded now.
You're right.

It's not arguing; it's debating. ;)

nikkiH
09-13-2005, 04:16 PM
And if you wanted an inner class, you needed to put fraction inside dsp1.
Right now, it's outside it.

Epison07
09-13-2005, 04:20 PM
kk, Thanks for the quick reply. So as of right now, my only problem is that I have two classes in one file. Thanks, I will try it out and see how it goes when I get home :-D

-Ep

Epison07
09-14-2005, 05:49 AM
Ok... Having a little trouble. As I said before, we have been going over the Java very fast. I tried the inner class, but Im sure thats not what he wants, plus I cant get it to work. I want it to just be in a seperate file. How do I "#include" it? I tried import, but I would assume import is for the pre made java classes.
So, on to my question. How do I "include" the fraction.java file with the main program file. Or do I have to change the extension of fraction to .class? Im not sure :-\ I thought he said that .class was the "compiled" version of the program, but yeah. Help? :-D

-Ep

EDIT: Never, I dont think you have to include the files, just have them in the same folder? Turns out, I forgot my main file was on my desktop and the fraction.java was in a seperate folder :p So yeah, I got different errors now, but Imma work on them more before I post. Wish me luck :-\

Epison07
09-14-2005, 06:07 AM
4. You are to add the source code for the class fraction to the file dsp1.java You should not change the class dsp1 and the main function in any way. To make sure you code works correctly, move dsp1.java to another directory, add the source code for the fraction class to dsp1.java. When completed, the file dsp1.java can be compiled into the two class files dsp1.class and fraction.class and the application should function in the exactly the same way as the downloaded class files.

5. Use email to turn in the project. Email the completed source file dsp1.java as an attachment


So he says only to turn in one file, and I am not to mess with that dsp1 class at all. I am a little lost, becuz I am back where I started. The fraction class in the main file with one stupid error :-\ Any idea what he is talking about, or how to get this to work?
/cry

nikkiH
09-14-2005, 03:33 PM
Sounds like he wants the inner class.

I have never used those, but I'd think just moving the code for the fraction class inside the class declaration of dsp1 would let it work. (you have it outside -- just move the whole block inside that last bracket instead)

But then he talks about two classes...

I'd ask him to clarify, personally.