473,396 Members | 1,921 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Java execute problem-Student ID class and ShowStudent class paired together

Create a class named Student. A student has fields for an ID number, number of credit hours earned, and number of points earned. (For example, many schools compute grade point averages based on a scale of 4, so a three-credit-hour class in which a student earns an A is worth 12 points. Also, create a class variable called lastStudentID which holds the last student ID number assigned to a student. Initialize it to 0. Each new student is assigned a student ID by adding one to this number. Update lastStudentID each time a student is created.

Next, include methods to assign values to all fields separately. For example, one method should assign ID number, another should assign credit hours earned, etc. A Student also has a field for grade point average. Include a method to compute the grade point average field by divinding points by credit hours earned. Write methods to display the values in each Student field.

Write a class named ShowStudent that instantiates a Student object from the class you created above. Set the necessary values and compute the Student grade point average, and then display all the values associated with the Student. Create three different students with different values.

I write it and it executes but then has errors in it..I dont know what the issue is I am using NetBeans
Expand|Select|Wrap|Line Numbers
  1.  package student;
  2.  
  3. /**
  4.  *
  5.  * @author rlewis07
  6.  */
  7. javac -cp . *.java 
  8. ...
  9. java -cp . ShowStudent
  10.  class Student
  11. {
  12.     // the private data members
  13.     private int IDnumber;
  14.     private int hours;
  15.     private int points;
  16.  
  17.     // constructor added in last part of project
  18.     Student()
  19.     {
  20.         IDnumber = 77375;
  21.         points = 12;
  22.         hours = 3;
  23.     }
  24.     // end of constructor
  25.  
  26.     // the public get and set methods
  27.     public void setIDnumber(int number)
  28.     {
  29.         IDnumber = number;
  30.     }
  31.  
  32.  
  33.     public int getPoints()
  34.     {
  35.         return points;
  36.     }
  37.  
  38.     // methods to display the fields
  39.     public void showIDnumber()
  40.     {
  41.         System.out.println("ID Number is: " + IDnumber);
  42.     }
  43.  
  44.     public void showHours()
  45.     {
  46.         System.out.println("Credit Hours: " + hours);
  47.     }
  48.  
  49.     public void showPoints()
  50.     {
  51.         System.out.println("Points Earned: " + points);
  52.     }
  53.  
  54.     public double getGradePoint()
  55.     {
  56.         return (double)points / hours;
  57.     }
  58.  
  59. }
  60.  
  61.  
my showStudent Code:
Expand|Select|Wrap|Line Numbers
  1. package student;
  2.  
  3. /**
  4.  *
  5.  * @author rlewis07
  6.  */
  7.  public class ShowStudent
  8. {
  9.     public static void main (String args[])
  10.     {
  11.         Student pupil = new Student();// 2 cannot resolve sybmol... points to 'S' in Student 
  12.  
  13.  
  14.         pupil.showIDnumber();
  15.         pupil.showPoints();
  16.         pupil.showHours();
  17.         System.out.println("The grade point average of the studnet created by constructor is "
  18.             + pupil.getGradePoint()+"\n\n");
  19.  
  20.         Student s2 = new Student();// 2 cannot resolve sybmol points to 'S in Student
  21.         s2.setIDnumber(12345);
  22.         s2.setPoints(66);
  23.         s2.setHours(20);
  24.         s2.showIDnumber();
  25.         s2.showPoints();
  26.         s2.showHours();
  27.         System.out.println("The grade point average of another student is "
  28.             + s2.getGradePoint()+"\n");
  29.  
  30.     }
  31. }
  32.  
Oct 28 '14 #1
3 9641
Frinavale
9,735 Expert Mod 8TB
It has been a long time since I've used Java but I believe in the file that contains the ShowStudent class you should be importing the student using the import keyword....

Check out this tutorial about Using Package Members.

-Frinny
Oct 28 '14 #2
Why if I do not have the constructor in my student class does it not run how do I assign the variables with out using a constructor? It runs but the values do not assign.
Looks like this:
run:
Student 1
ID Number is: 0
Points Earned: 0
Credit Hours: 0
Grade point average: NaN
Oct 28 '14 #3
Frinavale
9,735 Expert Mod 8TB
Your variables:
Expand|Select|Wrap|Line Numbers
  1.     private int IDnumber;
  2.     private int hours;
  3.     private int points;
Have a private scope which means that they are not accessible outside of the class where they were declared.

There are a few ways that you can chose to set these private variables from outside of the class (in your ShowStudent class's main method):
  1. Provide a constructor that lets you set them (which you said you aren't interested in)
  2. Change the scope of your variables to public so that you can access them outside of the class
  3. Implement public set... methods that can be called to set the private variables

In the code you posted here you are calling the following functions:
Expand|Select|Wrap|Line Numbers
  1.   s2.setIDnumber(12345);
  2.   s2.setPoints(66);
  3.   s2.setHours(20);
  4.  
Which implies to me that you are going for the last option:
implement public set... methods that can be called to set the private variables
This is actually a very smart way to implement the class because your set methods could do validation on the data that is about to be set to ensure that it is correct.

This, used in combination with the get methods, is a good safe way to ensure that the data in your class is correct.

So, in order to interact with the private member points from outside the class you are going to need the following 2 functions:
Expand|Select|Wrap|Line Numbers
  1. public void setPoints(int newPointsValue)
  2. {
  3.   points = newPointsValue;
  4. }
  5. public int getPoints()
  6. {
  7.   return points;
  8. }
That way you have the ability to set the private points member and the ability to get the value of the private points member from code that uses this class.

If you want to ensure that the points value contains a positive number you can do validation in the setPoints method to ensure that this is the case:
Expand|Select|Wrap|Line Numbers
  1. public void setPoints(int newPointsValue)
  2. {
  3.   if(newPointsValue < 0){
  4.    // here you need to throw exception so that the code stops executing
  5.    // you need a try catch block in the calling code that 
  6.    // catches the exception that you throw so that your application doesn't crash
  7.   }
  8.   points = newPointsValue;
  9. }
  10. public int getPoints()
  11. {
  12.   return points;
  13. }
Right now you are missing the set method according to the code that you posted which is why you can't set the values.


Now, if you were to change the scope to public you will be able to access the variables outside of the class in the ShowStudent class's main method.

After changing the scope of the class member variables to public, you would be able to set the members directly:
Expand|Select|Wrap|Line Numbers
  1.   Student pupil = new Student();
  2.   pupil.IDnumber=12345;
  3.   pupil.points = 66;
  4.   pupil.hours = 20;
  5.  
However, because you are not using the set method to set the values you will not be able to do validation to ensure that the data provided for the members is correct according to your business rules.

-Frinny
Oct 28 '14 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Robert Oschler | last post by:
Hello, I am a Python newbie (by experience, not chronologically :) ), so if any of this doesn't make sense my apologies in advance. I am reading the chapter in The Python Cookbook on databases...
17
by: Asfand Yar Qazi | last post by:
Basically this: //file 'B.hh' class B { protected: void f() {} }; //file 'C.hh'
4
by: John Cho | last post by:
Class Cartesian { double x; double y; public: Cartesian( ) { x = 0.0 ; y = 0.0;} Cartesian(double x1, double y1) { x = x1; y = y1;} }; class Polar { double radius;
4
by: Tadeusz S. | last post by:
Hello In Pascal, I use constructions like that var SomeVar = object SomeVar2 = object end; end; etc., so I could put my object into another
0
by: sql-db2-dba | last post by:
class V7Stp extends StoredProc { // (1) public void salaryModification (String table, ... ) throws Exception { ... } // (2) public void salaryAverage (String table, ... ) throws Exception
3
by: Jae | last post by:
Hello, here's what I want to do but I'm not sure if it will work because Im not sure how DB2 works with the JVM. Heres my setup: DB2---->UDF Class (lets call it A)----->Static Class (Lets call...
10
by: msnews.microsoft.com | last post by:
Public Class Controller Dim mx As New HelperClass 'here, where I have it now ???? Sub New() 'or here??? Dim mx As New HelperClass
9
by: DBC User | last post by:
Hi All, Would like to know which is the best approach. I have a class which has only constants variables (private) and a public static method, which returns a string variable. I would like to...
4
by: Gary li | last post by:
Hi, all I find "template template" class cann't been compiled in VC6 but can ok in Redhat9. I write a test program like as: template< template<class> class T> class A { }; int main() {...
3
by: puzzlecracker | last post by:
Would you quickly remind me the difference between, regular class, static class, and nested class? Thanks
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.