Class Design

Class Problem

  • (2 Pages)
  • +
  • 1
  • 2

15 Replies - 1348 Views - Last Post: 15 February 2010 - 12:27 PM Rate Topic: -----

#1 ElCam  Icon User is offline

  • New D.I.C Head

Reputation: -11
  • View blog
  • Posts: 39
  • Joined: 16-April 09

Class Design

Posted 15 February 2010 - 12:40 AM

My assignment is to create a student class that has fields for a student name, number, credits earned and quality points earned. I need mutators and accessors methods for the students name, students number and other fields that needs Initilizing. I believe I have done all those correctly in my class...but it's not compliling. My main program should return the gpa which is quality points divided by credits earned and outputs their name and their number of course.


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

Is This A Good Question/Topic? 0
  • +

Replies To: Class Design

#2 Guest_AlM*


Reputation:

Re: Class Design

Posted 15 February 2010 - 02:50 AM

Hi ElCam,

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");
Was This Post Helpful? 0

#3 amuridi  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 15-February 10

Re: Class Design

Posted 15 February 2010 - 02:55 AM

I'm "Guest_AlM", forgot to log in.

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.
Was This Post Helpful? 0
  • +
  • -

#4 japanir  Icon User is offline

  • jaVanir
  • member icon

Reputation: 1010
  • View blog
  • Posts: 3,025
  • Joined: 20-August 09

Re: Class Design

Posted 15 February 2010 - 04:54 AM

please post your code between code tags.
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);


Was This Post Helpful? 0
  • +
  • -

#5 ElCam  Icon User is offline

  • New D.I.C Head

Reputation: -11
  • View blog
  • Posts: 39
  • Joined: 16-April 09

Re: Class Design

Posted 15 February 2010 - 09:59 AM

View Postjapanir, on 15 February 2010 - 03:54 AM, said:

please post your code between code tags.
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?
Was This Post Helpful? 0
  • +
  • -

#6 ElCam  Icon User is offline

  • New D.I.C Head

Reputation: -11
  • View blog
  • Posts: 39
  • Joined: 16-April 09

Re: Class Design

Posted 15 February 2010 - 10:32 AM

Okay my student class has compiled and I really appreciate the help. But my main program that calls from my student program isn't compiling as easily. My compiling error is @ student = new Student1(name, Studnum, qualpts,earnedcreds );. I don't know what else I am doing wrong?



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! :)
Was This Post Helpful? 0
  • +
  • -

#7 n8wxs  Icon User is offline

  • --... ...-- -.. . -. ---.. .-- -..- ...
  • member icon

Reputation: 972
  • View blog
  • Posts: 3,878
  • Joined: 07-January 08

Re: Class Design

Posted 15 February 2010 - 10:34 AM

[ code] <- Paste Your Code between -> [ /code]


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

Was This Post Helpful? 0
  • +
  • -

#8 NoobKnight  Icon User is offline

  • D.I.C Head

Reputation: 49
  • View blog
  • Posts: 238
  • Joined: 14-July 09

Re: Class Design

Posted 15 February 2010 - 10:35 AM

Its hard to diagnose your problems without compile errors or the latest code. But...

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...
Was This Post Helpful? 0
  • +
  • -

#9 ElCam  Icon User is offline

  • New D.I.C Head

Reputation: -11
  • View blog
  • Posts: 39
  • Joined: 16-April 09

Re: Class Design

Posted 15 February 2010 - 10:56 AM

Ok here are my latest code and issues. Here are both my class and my main program. My class is compiling without any issues. :)

   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!!
Was This Post Helpful? 0
  • +
  • -

#10 NoobKnight  Icon User is offline

  • D.I.C Head

Reputation: 49
  • View blog
  • Posts: 238
  • Joined: 14-July 09

Re: Class Design

Posted 15 February 2010 - 11:33 AM

earndcreds = Keyboard.nextDouble();
System.out.println(gpa)                    // <--- Missing ;
student = new Student1(name, studnum2 );


Was This Post Helpful? 0
  • +
  • -

#11 ElCam  Icon User is offline

  • New D.I.C Head

Reputation: -11
  • View blog
  • Posts: 39
  • Joined: 16-April 09

Re: Class Design

Posted 15 February 2010 - 11:48 AM

NoobKnight Thank you soo much! I really appreciate it!

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

Was This Post Helpful? 0
  • +
  • -

#12 NoobKnight  Icon User is offline

  • D.I.C Head

Reputation: 49
  • View blog
  • Posts: 238
  • Joined: 14-July 09

Re: Class Design

Posted 15 February 2010 - 12:09 PM

Its good practice to initialize all fields before you use them.

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

Was This Post Helpful? 0
  • +
  • -

#13 ElCam  Icon User is offline

  • New D.I.C Head

Reputation: -11
  • View blog
  • Posts: 39
  • Joined: 16-April 09

Re: Class Design

Posted 15 February 2010 - 12:11 PM

I reworked my code but its still not compiling any ideas??

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!! :)

View PostNoobKnight, on 15 February 2010 - 11:09 AM, said:

Its good practice to initialize all fields before you use them.

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!
Was This Post Helpful? 0
  • +
  • -

#14 NoobKnight  Icon User is offline

  • D.I.C Head

Reputation: 49
  • View blog
  • Posts: 238
  • Joined: 14-July 09

Re: Class Design

Posted 15 February 2010 - 12:16 PM

Crud, read the previous post I had. I accidentally hit send before I was ready and then edited it. But assign the doubles with 0.0 at least.
Was This Post Helpful? 0
  • +
  • -

#15 japanir  Icon User is offline

  • jaVanir
  • member icon

Reputation: 1010
  • View blog
  • Posts: 3,025
  • Joined: 20-August 09

Re: Class Design

Posted 15 February 2010 - 12:17 PM

you perform the calculation to get the gpa when both quality and credits have no value.
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

Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2