PDA

View Full Version : Marks.java


gingergib
12-05-2006, 06:10 PM
i am being told that "class" or "interface" expected for lines 8,11,42,42. i have no idea why. any help is accepted, here is the code i have written:

public class Marks
{
public static void main(String args[]);
{
int[] marks = enterMarks();
}
}
static int counter=20;


public static int[] enterMarks()
{
int classMarks[]= new int[counter];

for (int i=0; i<counter; i++)
{
classMarks[i] = getScannerInput.anInt("Please enter an integer value between 0 and 20 for each mark :");
}

System.out.println();
System.out.print("The marks entered were ");

for (int j=0; j><counter; j++)
{
System.out.print(classMarks[j]+", ");
}
return classMarks;
}


public static int max(int arr[])
{
int[] marks = classMarks;
int tempStore=0;
for (int k=0;k<counter;k++)
{
if(marks[k]>marks[k+1])
tempStore=marks[k];
}
return tempStore;
}
}

Spookster
12-05-2006, 06:31 PM
Looks like you have several issues here with your code. The first one being you have an extra parenthesis at the beginning that is closing your class early.


}
static int counter=20;Also what is this?

getScannerInput.anInt

Is that method part of another class that you are using? How are you attempting to implement this other class? I say that because you cannot just call the method without implementing the other class.

gingergib
12-05-2006, 07:33 PM
a yes, dont know how i missed the clsing bracket!! feel like an idiot now lol. in line 23 (i pasteded it below) it asks for a ">"?? i cant see where to put it , i thought i already had enough?? cheers
for (int j=0; j><counter; j++)

gingergib
12-05-2006, 07:35 PM
getScannerInput.anInt is what i use for user inputing a value. is it wrong? should i be using something else??

Spookster
12-05-2006, 08:40 PM
a yes, dont know how i missed the clsing bracket!! feel like an idiot now lol. in line 23 (i pasteded it below) it asks for a ">"?? i cant see where to put it , i thought i already had enough?? cheers
for (int j=0; j><counter; j++)

You can't use a greater than and less than operation at the same time. Typically in the second parameter of a for loop you are going to use one of three operations... > (greater than), >= (greater than or equal), < (less than), <= (less than or equal).

gingergib
12-05-2006, 08:54 PM
this is really startin to get to me. i need to write a program which will allow a user to input 20 peoples marks from a test. then it will give me the average the minimum mark the maximum mark and all the marks above 8(which will be the pass mark). im using a method called "enterMarks() to create a new array called classMarks which will ask the user to input the 20 marks between 0-20. my output was supposed to show all the above information but i just cant get it right!! please, any help is more than appreciated. this is my code so far, but i think the "getScannerInput" is wrong:
public class Marks
{
public static void main(String args[]);
{
int[] marks = enterMarks();
}
static int counter=20;


public static int[] enterMarks()
{
int classMarks[]= new int[counter];

for (int i=0; i<counter; i++)
{
classMarks[i] = getScannerInput.anInt("Please enter an integer value between 0 and 20 for each mark :");
}

System.out.println();
System.out.print("The marks entered were ");

for (int j=0; j>=counter; j++)
{
System.out.print(classMarks[j]+", ");
}
return classMarks;
}


public static int max(int arr[])
{
int[] marks = classMarks;
int tempStore=0;
for (int k=0;k<counter;k++)
{
if(marks[k]>marks[k+1])
tempStore=marks[k];
}
return tempStore;
}
}

Spookster
12-05-2006, 10:07 PM
Where is this getScannerInput defined? Are you importing anything? Are you extending another class? We don't know where that is coming from or what is does. You are using it as if it is a class but I do not see the class definition or declaration for it.

daniel_g
12-05-2006, 10:32 PM
From first sigth, it looks like your trying to do the whole thing at once. Start step by step, for instance, the very first thing I do when creating a class is:

public class Marks{
public static void main(String[] args);
{
}
}

Then I complile to see if there are no mistakes. When compiled, I get the error:

Marks.java:3: missing method body, or declare abstract
public static void main(String[] args);
That means there's something wrong with "public static void main(String[] args);"
I reason that java is telling me that I'm calling main() but it has no method. Why? Because of the semicolon. So the next step is to remove the semicolon.

Once you fix that, continue with your work. At this point, your code should look like:

public class Marks
{
public static void main(String[] args)
{
int[] marks = enterMarks();

}
static int counter=20;


public static int[] enterMarks()
{
int classMarks[]= new int[counter];

for (int i=0; i<counter; i++)
{
classMarks[i] = getScannerInput.anInt("Please enter an integer value between 0 and 20 for each mark :");
}

System.out.println();
System.out.print("The marks entered were ");

for (int j=0; j>=counter; j++)
{
System.out.print(classMarks[j]+", ");
}
return classMarks;
}
}

with this error:

Marks.java:17: cannot find symbol
symbol : variable getScannerInput
location: class Marks
classMarks[i] = getScannerInput.anInt("Please enter an integer value between 0 and 20 for each mark :");

This leads back to the question Spook asked you.
Usually, if you want to define a scanner, you do it this way:

import java.util.Scanner;
your class{
Scanner yourVariable = new Scanner(System.in);
your method(){
System.out.println("Ask user to do something here");
int something = yourVariable.nextInt();
}
}

I guess you can still use getScannerInput(), but then make sure you know where it's coming from, and how to import it.

gingergib
12-06-2006, 03:52 PM
ok, i have sat all night with what you guys have said an come up with the following:
public class Marks
{
public static int enterMarks()
{
int classMarks[];
classMarks = new int[20];
int marks = getScannerInput.adouble("Enter the 30 marks");
{
System.out.println("The class exam marks are: " + classMarks + );
}
{
public static void main(String args[])
{
int[] marks = enterMarks();

public static int Max()
{

System.out.println("The maximum mark in the class was: " + Max + );
}
public static int Min()
{

System.out.println("The minimum mark in the class was: " + Min + );
}

public static double Average()
{
System.out.printf("The class average correct to 1 decimal place is: " + Average + );
}

public static int Passed()
{
if(Marks =>8)
Count;
System.out.println("The number of students who passed the exam is " + Passed + );
}
}
}
}
}

it tells me that for line 9: ")" expected and illegal start of expression,
and for line 12: illegal start of expression,
and for line 39: ";" expected.
this is as far as my mind can physically go now so cheers for all your help guys and if u can solve this for me please feel free to respond

Gox
12-06-2006, 04:09 PM
You appear to have some bracket issues in the you code.
I'm not sure what you're trying to do with this section of code.

{
System.out.println("The class exam marks are: " + classMarks + );
}
{

Also, you can't declare methods inside the body of "main"

...
public static void main(String args[])
{
int[] marks = enterMarks();

public static int Max()
{

System.out.println("The maximum mark in the class was: " + Max + );
}
...

I've rearranged your code to something that has the proper structure, but it still has issues, and probably won't compile. But work starting from here as it's closer in terms of correctness.
public class Marks
{
public static int enterMarks()
{
int classMarks[];
classMarks = new int[20];
int marks = getScannerInput.adouble("Enter the 30 marks");
System.out.println("The class exam marks are: " + classMarks + );
}

public static int Max()
{
System.out.println("The maximum mark in the class was: " + Max + );
}
public static int Min()
{
System.out.println("The minimum mark in the class was: " + Min + );
}

public static double Average()
{
System.out.printf("The class average correct to 1 decimal place is: " + Average + );
}

public static int Passed()
{
if(Marks =>8)
Count;
System.out.println("The number of students who passed the exam is " + Passed + );
}

public static void main(String args[])
{
int[] marks = enterMarks();
}

}

I'll come back and give you some more pointers later tonight when I have more time.
But quickly I'll mention that all your methods declare return types, but then never return any value

public static double Average()
{
System.out.printf("The class average correct to 1 decimal place is: " + Average + );
}

You should either declare the method as void (i.e that it won't return anything) or you'll need to return a value. The Java compiler won't let you compile your program until you do one of these things.

public static void Average()
{
System.out.printf("The class average correct to 1 decimal place is: " + Average + );
}

OR

public static double Average()
{
double avg;

... //Some code to calculate the Average, then

return avg
}

Spookster
12-06-2006, 04:15 PM
Honestly there is so much wrong with the code you just posted I don't know where to begin. Obviously this is a homework assignment so we are not going to just write it for you. I think you are trying to do too much at one time. The first step to writing a program like this is to figure out how to accomplish what it is you need to do. You should not be worrying about how to write the code at this point. Write algorithms and pseudocode to layout the design of your program and figure out the logic.

Then when you get to writing the code take it one step at a time. Write the skeleton for the main class with it's main method. Compile and make sure you don't have errors at that point. Then one by one write each additional method you will need to use and compile each time to make sure you don't have errors. Once you have all the necessary methods written use those methods in your main method using the logic you came up with in the algorithms and pseudocode that you wrote.

In your code right now you have too many syntax errors to list. You should properly format your code with proper tabbing. Use a text editor that highlights code. This helps to see potential typos. Your biggest problem right now is your use of braces {}. You are not closing methods, your are enclosing methods inside other methods, other braces are just out of place or unecessary.

A structure of a class should be something like this:


public class ClassName() {

public static void main (String args[]) {


}//End main

int method1() {

}//End method1

int method2() {

}//End method2
} // End ClassName

It is sometimes helpful to put comments after your closing braces to indicate which method/class the brace belongs to.

Gox
12-06-2006, 06:13 PM
...I think you are trying to do too much at one time...write each additional method you will need to use and compile each time to make sure you don't have errors...

I've quoted this because I think that it is a very helpful tip when writing code. Whether you're a beginner or a pro I think using this methodology is very helpful. It helps eliminate the problem of "not being able to see the trees for the forest".

Even if you were able to write a complete program without any syntax errors and compile at the end, you'll find that you'll most likely have to still deal with some logic issues. If you compile once at the end, you'll have no idea where to start looking. Compiling incrementally will help cut down some wasted time as you'll already have a good indication of which parts/methods of the program work properly.

gingergib: Continue working on your code using the tips provided and let us know how you progress. We're here to help.

gingergib
12-07-2006, 10:21 PM
well the deadline was yesterday and i didnt get the program done. i got 28%(40% is the pass mark) for my program. which although dosnt sound like much is much more than what i cuda got without your help guys(and girls if any girls helped me out). i got my marks for havin the basics of the code, even tho they wer in the wrong places and the program didnt execute. i got 100% in my other 3 programs so that gives me a pass over all for my first semester. other 3 programs wer much easier. 1) a program that wen a year is entered states wether its a leap yr or not, 2) a program that wen 3 angles are entered states wether its a right angled, isosceles or equalateral triangle, and 4) gives the prime numbers up to the limit entered by the user(200 was the limit i had to work with). i failed the 3rd one. one final question: any good E-books or free software or anything that you would recommend so as i can get 100% next time?
cheers again, and i hope i never to have to write that program again in my life!!
(p.s. you guys must be geniouses at programin to have the time to volunteer your services!!)