Class DesignClass Problem
15 Replies - 1036 Views - Last Post: 15 February 2010 - 12:27 PM
#1
Class Design
Posted 15 February 2010 - 12:40 AM
Here is my code:
// This stimulates a student class.
public class Student1
{
private String name;
private String studnum;
private double qualpts;
private double earnedcreds;
}
public Student1 (String Name ,
String Studnam)
{
name = Name;
studnam = Studnam;
qualpts = 0.0;
earnedcreds = 0.0;
}
public double getgpa()
{
return qualpts/earnedcreds;
}
It is having problems @ public student1 (String Name, String ) The error I am getting is unexpected';'..?? A
And here is my main program that is calling my class.
Code:
import java.util.Scanner;
public class Schuler
{
public static void main(String[] args)
{
Student1 Student;
double credits;
double quality;
double gpa;
String name;
String studnum2;
double earndcreds;
Scanner Keyboard = new Scanner(System.in);
System.out.print("What is the Students name?");
name = Keyboard.nextLine();
System.out.print("What is the Students number?");
studnum2 = Keyboard.nextLine();
System.out.print("What are the quality points earned?");
earndcreds = Keyboard.nextDouble();
System.out.print(gpa);
Student = new Student1(Name,Studnam);
// Creates new Student # 2
System.out.print("What is the Students name?");
name = Keyboard.nextLine();
System.out.print("What is the Students number?");
studnum2 = Keyboard.nextLine();
System.out.print("What are the quality points earned?");
earndcreds = Keyboard.nextDouble();
System.out.println(gpa)
Student = new Student1(Name, Studnam);
// Creates Student # 3
System.out.print("What is the Students name?");
name = Keyboard.nextLine();
System.out.print("What is the Students number?");
studnum2 = Keyboard.nextLine();
System.out.print("What are the quality points earned?");
earndcreds = Keyboard.nextDouble();
System.out.println(gpa);
}
}
The error I am getting here is when I create my new student...Thank you for any help! I really appreciate it
Replies To: Class Design
#2 Guest_AlM*
Re: Class Design
Posted 15 February 2010 - 02:50 AM
I'm learning as I go along. (Beginner here)..
As I'm looking through your code, my first question is.. What's the point in creating the exact same variable for your main program when you already taken care of those in your tester (driven-program as some might say (I think that's the right word)) class.
Instead of having "String name" in your Schuler as you do in your Student1 class, I would erase that variable and use the getter/setter in your Student1 class.
For example,
(Underneath getGpa).. You might write something like this..
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
And in your Schuler class would have something like..
---------------
public static void main (String[] args)
{
Student1 s = new Student1();
Scanner scan = new Scanner(System.in);
System.out.println("What is the student name?");
String name = scan.nextLine();
s.setName(name);
System.out.println("Your name is: " + s.getName());
------------
Atleast it would show your instructor that your integrating the getter/setters from the tester..
Or you could just use the toString... for example;
public String toString()
{
return ("Student name: " + name + " , Student Number: " + studnum + " , Credits Earned: " + earnedcreds + ", GPA: " + getGpa());
}
-----------
When your creating student, sure you will have errors. Cause you have this in your code:
Student = new Student1(Name, Studnam); ..
If you wrap around the Name and Studnam with double quote, the error goes away. Why? Because remember in your Student1 class, you type...
public Student1(String name, String Studnam) .. It's expecting you to have the parameter as String, and it's only a String when you have double quote wrapped around the words...
Example (in your Schuler class).. instead of >> Student = new Student1(Name, Studnam); << .. you could say Student = new Student1("ElCam", "Jackson");
#3
Re: Class Design
Posted 15 February 2010 - 02:55 AM
Just pointing you in the right direction, if your instructor wants you to set the accessor and mutator, I think he's expecting you to integrate that into your main program.
I've got issues of myself. My instructor wants me to do a LowBall game written in java, it's easy but it'll be easier if I know how the hell LowBall is played.
Good luck.
#4
Re: Class Design
Posted 15 February 2010 - 04:54 AM
from glancing at your code, i noticed that you declare the variables:
String studnum2; String name;
however, later in your code you use it as: "Studnum" which is not declared. and "Name" which also you didnt declare. (when you create the Student1 Objects):
Student = new Student1(Name,Studnam);
#5
Re: Class Design
Posted 15 February 2010 - 09:59 AM
japanir, on 15 February 2010 - 03:54 AM, said:
from glancing at your code, i noticed that you declare the variables:
String studnum2; String name;
however, later in your code you use it as: "Studnum" which is not declared. and "Name" which also you didnt declare. (when you create the Student1 Objects):
Student = new Student1(Name,Studnam);
Thank you both for your ideas. But I still have to call from my student1 file to my schuler so aren't those variables the same in both? And also I still am having trouble compiling...is my syntax correct and is my methodology correct also?
#6
Re: Class Design
Posted 15 February 2010 - 10:32 AM
import java.util.Scanner;
public class Schuler
{
public static void main(String[] args)
{
Student1 student;
double credits;
double gpa;
double quality;
String name;
String studnum2;
double earndcreds;
gpa = quality/earndcreds;
Scanner Keyboard = new Scanner(System.in);
System.out.print("What is the Students name?");
name = Keyboard.nextLine();
System.out.print("What is the Students number?");
studnum2 = Keyboard.nextLine();
System.out.print("What are the quality points earned?");
earndcreds = Keyboard.nextDouble();
System.out.print(gpa);
student = new Student1(name, Studnum, qualpts,earnedcreds );
// Creates new Student # 2
System.out.print("What is the Students name?");
name = Keyboard.nextLine();
System.out.print("What is the Students number?");
studnum2 = Keyboard.nextLine();
System.out.print("What are the quality points earned?");
earndcreds = Keyboard.nextDouble();
System.out.println(gpa)
student = new Student1(name, Studnum, qualpts,earnedcreds);
// Creates Student # 3
System.out.print("What is the Students name?");
name = Keyboard.nextLine();
System.out.print("What is the Students number?");
studnum2 = Keyboard.nextLine();
System.out.print("What are the quality points earned?");
earndcreds = Keyboard.nextDouble();
System.out.println(gpa);
}
}
I hope to get the hang of the code snippet things. And again, Thanks for your help and taking the time to look @ my code!
#7
Re: Class Design
Posted 15 February 2010 - 10:34 AM
The blue angle brackets symbol above the edit window will insert the tags in the window at the cursor when you click on them.
This post has been edited by n8wxs: 15 February 2010 - 10:40 AM
#8
Re: Class Design
Posted 15 February 2010 - 10:35 AM
These are definitely wrong.
public static void main(String[] args)
{
Student1 Student;
double credits;
double quality;
double gpa;
[b]String name;[/b]
[b]String studnum2;[/b]
double earndcreds;
Scanner Keyboard = new Scanner(System.in);
...
Student = new Student1([b]Name,Studnam[/b]); // should be (name, studnum2)
I also think the class statement is wrong...
#9
Re: Class Design
Posted 15 February 2010 - 10:56 AM
public class Student1
{
private String name;
private String Studnum;
private double qualpts;
private double earnedcreds;
public Student1 (String startname ,
String startnum)
{
name = startname;
Studnum = startnum;
qualpts = 0.0;
earnedcreds = 0.0;
}
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
public String getNum()
{
return Studnum;
}
public void setNum()
{
this.Studnum = Studnum;
}
public double getgpa()
{
return qualpts/earnedcreds;
}
}
My main program that calls my class program.
import java.util.Scanner;
public class Schuler
{
public static void main(String[] args)
{
Student1 student;
double credits;
double gpa;
double quality;
String name;
String studnum2;
double earndcreds;
gpa = quality/earndcreds;
Scanner Keyboard = new Scanner(System.in);
System.out.print("What is the Students name?");
name = Keyboard.nextLine();
System.out.print("What is the Students number?");
studnum2 = Keyboard.nextLine();
System.out.print("What are the quality points earned?");
earndcreds = Keyboard.nextDouble();
System.out.print(gpa);
student = new Student1(name, studnum2 );
// Creates new Student # 2
System.out.print("What is the Students name?");
name = Keyboard.nextLine();
System.out.print("What is the Students number?");
studnum2 = Keyboard.nextLine();
System.out.print("What are the quality points earned?");
earndcreds = Keyboard.nextDouble();
System.out.println(gpa)
student = new Student1(name, studnum2 );
// Creates Student # 3
System.out.print("What is the Students name?");
name = Keyboard.nextLine();
System.out.print("What is the Students number?");
studnum2 = Keyboard.nextLine();
System.out.print("What are the quality points earned?");
earndcreds = Keyboard.nextDouble();
System.out.println(gpa);
}
}
import java.util.Scanner; //Author: Eliot Ostling
public class Schuler
{
public static void main(String[] args)
{
Student1 student;
double credits;
double gpa;
double quality;
String name;
String studnum2;
double earndcreds;
gpa = quality/earndcreds;
Scanner Keyboard = new Scanner(System.in);
System.out.print("What is the Students name?");
name = Keyboard.nextLine();
System.out.print("What is the Students number?");
studnum2 = Keyboard.nextLine();
System.out.print("What are the quality points earned?");
earndcreds = Keyboard.nextDouble();
System.out.print(gpa);
student = new Student1(name, studnum2 );
// Creates new Student # 2
System.out.print("What is the Students name?");
name = Keyboard.nextLine();
System.out.print("What is the Students number?");
studnum2 = Keyboard.nextLine();
System.out.print("What are the quality points earned?");
earndcreds = Keyboard.nextDouble();
System.out.println(gpa)
student = new Student1(name, studnum2 );
// Creates Student # 3
System.out.print("What is the Students name?");
name = Keyboard.nextLine();
System.out.print("What is the Students number?");
studnum2 = Keyboard.nextLine();
System.out.print("What are the quality points earned?");
earndcreds = Keyboard.nextDouble();
System.out.println(gpa);
}
}
[/code]
My last compiling error is @ line 36. And it says that there is unexpected ';' @ line 36 so I don't know how to proceed. Again thanks for looking @ my code and helping me out! You guys rock!!
#10
Re: Class Design
Posted 15 February 2010 - 11:33 AM
earndcreds = Keyboard.nextDouble(); System.out.println(gpa) // <--- Missing ; student = new Student1(name, studnum2 );
#11
Re: Class Design
Posted 15 February 2010 - 11:48 AM
Also, new problem.
import java.util.Scanner; //Author: Eliot Ostling
public class Schuler
{
public static void main(String[] args)
{
Student1 student;
double credits;
double gpa;
double quality;
String name;
String studnum2;
double earndcreds;
gpa = quality/earndcreds;
Scanner Keyboard = new Scanner(System.in);
System.out.print("What is the Students name?");
name = Keyboard.nextLine();
System.out.print("What is the Students number?");
studnum2 = Keyboard.nextLine();
System.out.print("What are the quality points earned?");
earndcreds = Keyboard.nextDouble();
System.out.print(gpa);
student = new Student1(name, studnum2 );
// Creates new Student # 2
System.out.print("What is the Students name?");
name = Keyboard.nextLine();
System.out.print("What is the Students number?");
studnum2 = Keyboard.nextLine();
System.out.print("What are the quality points earned?");
earndcreds = Keyboard.nextDouble();
System.out.println(gpa);
student = new Student1(name, studnum2 );
// Creates Student # 3
System.out.print("What is the Students name?");
name = Keyboard.nextLine();
System.out.print("What is the Students number?");
studnum2 = Keyboard.nextLine();
System.out.print("What are the quality points earned?");
earndcreds = Keyboard.nextDouble();
System.out.println(gpa);
}
}
Sorry for the last post..this is my error. Its on line 15 it says they weren't initilized? But I have them there as doubles? Is there something I am not seeing??
This post has been edited by ElCam: 15 February 2010 - 11:52 AM
#12
Re: Class Design
Posted 15 February 2010 - 12:09 PM
something like...
Student1 student = null;
double credits = 0.0;
double gpa = 0.0;
double quality = 0.0;
String name = null;
String studnum2 = null;
double earndcreds = 0.0;
This post has been edited by NoobKnight: 15 February 2010 - 12:10 PM
#13
Re: Class Design
Posted 15 February 2010 - 12:11 PM
import java.util.Scanner;
public class Schuler
{
public static void main(String[] args)
{
Student1 student;
double credits;
double gpa;
double quality;
String name;
String studnum2;
double earndcreds;
Scanner Keyboard = new Scanner(System.in);
gpa = quality/credits
System.out.print("What is the Students name?");
name = Keyboard.nextLine();
System.out.print("What is the Students number?");
studnum2 = Keyboard.nextLine();
System.out.print("What are the quality points earned?");
quality = Keyboard.nextDouble();
System.out.print("How many credits have you earned?");
credits = Keyboard.nextDouble();
System.out.print(name + studnum2 + gpa);
student = new Student1(name, studnum2 );
// Creates new Student # 2
System.out.print("What is the Students name?");
name = Keyboard.nextLine();
System.out.print("What is the Students number?");
studnum2 = Keyboard.nextLine();
System.out.print("What are the quality points earned?");
quality = Keyboard.nextDouble();
System.out.print("How many credits have you earned?");
credits = Keyboard.nextDouble();
System.out.println(name + studnum2 + gpa);
student = new Student1(name, studnum2 );
// Creates Student # 3
System.out.print("What is the Students name?");
name = Keyboard.nextLine();
System.out.print("What is the Students number?");
studnum2 = Keyboard.nextLine();
System.out.print("What are the quality points earned?");
quality = Keyboard.nextDouble();
System.out.print("How many credits have you earned?");
credits = Keyboard.nextDouble();
System.out.println(name + studnum2 + gpa);
}
}
Thanks everybody so far for your suggestions and ideas. It really has helped me a bunch!!
NoobKnight, on 15 February 2010 - 11:09 AM, said:
something like...
Student1 student;
double credits;
double gpa;
double quality;
String name;
String studnum2;
double earndcreds;
Yeah. That's excatly what I had in my code...but for some odd reason it wasn't picking it up. So my code I reworked it but its still giving me the same issues? All the figures i get are suppose to be from the user so I don't understand where I am going wrong. Thanks a bunch!
#14
Re: Class Design
Posted 15 February 2010 - 12:16 PM
#15
Re: Class Design
Posted 15 February 2010 - 12:17 PM
do the calculation after getting the user input for these variables:
public static void main(String[] args)
{
Student1 student;
double credits;
double gpa;
double quality;
String name;
String studnum2;
double earndcreds;
Scanner Keyboard = new Scanner(System.in);
///<<remove the gpa calculator from here
System.out.print("What is the Students name?");
name = Keyboard.nextLine();
System.out.print("What is the Students number?");
studnum2 = Keyboard.nextLine();
System.out.print("What are the quality points earned?");
quality = Keyboard.nextDouble();
System.out.print("How many credits have you earned?");
credits = Keyboard.nextDouble();
//calculate gpa after getting quality and credits
gpa = quality/credits
System.out.print(name + studnum2 + gpa);
student = new Student1(name, studnum2 );
repeat this change with the rest of the Students you create.
**edited typo
This post has been edited by japanir: 15 February 2010 - 12:18 PM
|
|

New Topic/Question
Reply




MultiQuote




|