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