Skip to main content

Can some please help (new Thread ) please disregard the previous

3 replies [Last post]
safarirover
Offline
Joined: 2007-06-28

I am new to Java and programing as a whole. For me it Java has been difficult to understand. Currently I have about 20 hours on a project that would take someone who understands Java about 10 minutes. I am taking Java online and the campus is over 500 miles away so I can't raise my hand for a question. any assiatnce would be greatly appreciated.

The error message

Student.java:92: ';' expected
public String getaCatagoryByName()
^
1 error

When I correct the error and compile I get 18 errors..........

The Code

public class Student
{

/*
Instance Fields
*/

private int age; // This is the student's age
private double gpa; // This is the student's GPA
private String name; // This is the student's name
private int credithours; //This is the students credit hours

/*

Constructor for the default student.

*/

public Student()

{
age=0;
gpa=0.0;
name="";
credithours=0;

}

/**
Constructs a Student with all data provided
@param theAge is the student's current age
@param theGPA is the student's current Grade Point Average (GPA)
@param theName is the student's first name
@param theCreditHours is the student's complted number of course hours with a successful grade

*/

public Student(int theAge, double theGPA, String theName, int theCreditHours)

{
age=theAge;
gpa=theGPA;
name=theName;
credithours=theCreditHours;

}

public int getAge()

{
return age;
}

public void setAge(int theAge)

{
age=theAge;
}

public int getaCatagoryByNumber()

{
int age=0;
int aCatagoryByNumber=0;
if (age <=30)aCatagoryByNumber=aCatagoryByNumber +0;
else if (age <=50)aCatagoryByNumber=aCatagoryByNumber + 8;
else if (age >50)aCatagoryByNumber=aCatagoryByNumber +16;

return aCatagoryByNumber;
}

public void setaCatagoryByNumber(int aCatagoryByNumber)

{
age=aCatagoryByNumber;
}

public String getaCatagoryByName()

String aCatagoryByName="";

{
if (aCatagoryByNumber== 0) aCatagoryByName="Young";
else if (aCatagoryByNumber == 8) aCatagoryByName="Middle";
else if (aCatagoryByNumber == 16) aCatagoryByName="Mature";

return aCatagoryByName;
}

public void setaCatagoryByName(String aCatagoryByName)

{
age=aCatagoryByName;
}

public double getGPA()

{
return gpa;
}

public void setGPA(double theGPA)

{
gpa=theGPA;
}

public double getaGPARating()

{
double gpa=0.0;
double aGPARating=0.0;
if (gpa <=2.0)aGPARating=aGPARating +0;
else if (gpa <=3.5)aGPARating=aGPARating +1;
else if (gpa >3.5)aGPARating=aGPARating +2;

return aGPARating;
}

public void setaGPARating(double aGPARating)

{
gpa=aGPARating;
}

public String getaGPAByName();

String aGPAByName="";
{
if (aGPAByName == 0) aGPAByName="Probation";
else if (aGPAByName == 1) CatagoryByName="Passing";
else if (aGPAByName == 16) aGPAByName="Honors";

return aGPAByName;
}

public void setaGPAByName(String aGPAByName)

{
gpa=aGPAByName;
}

public String getName()

{
return name;

}

public void setName(String aName)

{
name=aName;
}

public int getCreditHours()

{
return credithours;
}

public void setCreditHours(int theCreditHours)

{
credithours=theCreditHours;
}

public int getaCreditHoursRating()

{
int credithours=0;
int aCreditHoursRating=0;
if (credithours >60)aCreditHoursRating=aCreditHoursRating +0;
else if (credithours >60)aCreditHoursRating=aCreditHoursRating +4;

return aCreditHoursRating;
}

public void setaCreditHoursRating(int aCreditHoursRating)

{
credithours=aCreditHoursRating;
}

public String getaCreditHoursByName();

String aCreditHoursByName="";
{
if (aCreditHoursByName == 0) aCreditHoursByName="Lower Division";
else if (aCreditHoursByName == 4) aCreditHoursByName="Upper Division";

return aCreditHoursByName;
}

public void setaCreditHoursByName(String aCreditHoursByName)

{
credithours=aCreditHoursByName;
}

}

The Assignment

Here is the assignment
Project Objectives:

To continue practicing writing Java classes.

To practice using if/else statements in Java.

Preparatory Readings:

Big Java, chapters 2-4 and 6

Project Requirements:

The task for this project is to write a Java class to represent a college student. The name of the class MUST be Student. Each student will have a name (first name will be fine for this project), an age, a number of credit hours and a GPA. You can determine the data types for those values by looking at the methods described below. Notice that you MUST use the exact names for the methods listed below, but you need not use the same names for the parameters. The parameters must however, be listed in exactly the same order as below.

There are two other values needed for each student. These last two values can be determined from the four values (age, GPA, name, hours) already mentioned. The first is whether the student is upper division or lower division. That is based on the number of credit hours the student has successfully completed. For this project we will consider any student with 60 credit hours or more to be upper division. Less than 60 hours will be lower division. You need to have a method in your class called isUpperDivision that will return a boolean value to answer that question.

The final piece of information that your class must be able to provide is the category of the student. (This is strictly something created for the purposes of this project. It will force you to do some thinking, write some logical code and use if/else statements.) Here are the categories we will use. We will divide students into three groups based on their age. Students below thirty are Young, students at least thirty, but below fifty are considered Middle and students fifty or over are Mature. Next we will divide students into two groups based on credit hours. As described above students will be either Lower Division or Upper Division. Finally, we divide students into three groups based on GPA. Students with a GPA below 2.0 will be called Probation. Students with a GPA of at least 2.0, but less than 3.5 will be considered Passing and students with a GPA of 3.5 or more will be considered Honor students. For example, a student age 22 with 35 credit hours and a GPA of 2.75 would be in this category: Young Lower Division Passing.

There are actually 18 different possible categories for students. For the class that you are writing we will also assign a numeric value for each category. Here is how you are to assign these numeric values. Think of each category as being worth some number of points. Here is how points are to be assigned:

The three age groups:

Young = 0 points

Middle = 8 points

Mature = 16 points

The two credit hour groups:

Lower Division = 0 points

Upper Division = 4 points

The three GPA groups:

Probation = 0 points

Passing = 1 points

Honors = 2 points

Therefore, a person in the category: Young Lower Division Passing

would have a numeric category of 1.

A person in the category: Middle Upper Division Honors

would have a numeric category of 14.

(This point system is simply a choice of the instructor. Dont worry if it seems strange.)

Now you need two more methods to return the students category. The first will be called getCategoryByName and will return the category as a String of characters. (ex. Young Lower Division Passing)

The second will be called getCategoryByNumber and will return a single integer. If the student is Middle Upper Division Honors, it will return the integer 14.

Here are the methods that your class must include:

// a default constructor which assigns reasonable default values

public Student()

// a constructor which assigns the four necessary values

public Student (int theAge, double theeGPA, String theName, int theCreditHours)

// getters and setters for age, GPA, name and credit hours

public int getAge()

public void setAge(int theAge)

public double getGPA()

public void setGPA(double theGPA)

public String getName()

public void setName(String aName)

public int getCreditHours()

public void setCreditHours (int theHours)

// a method to return true or false to the question, is the student upper division?.

public boolean isUpperDivision()

// a method to return the category as three words

public String getCategoryByName()

// a method to return the category as an integer

public int getCategoryByNumber()

You may use any names you like for instance fields and you may have as many private methods as you like. In previous projects you probably have not used private methods, but they are simply methods that are only called from within the class. They are not accessible form outside the class. They are not part of the public interface.

Here is a link to the java file for the StudentTest class.

It does not test everything in your Student class, but gets you off and running.

Here is the output from a run of the StudentTest class:

S1 Category = 0

S1 Category = Young : Lower Division : Probation

S2 Category = 14

S2 Category = Middle : Upper Division : Honors

S1 Category = 8

S1 Category = Middle : Lower Division : Probation

s2 name = Joe

S2 Category = 21

S2 Category = Old : Upper Division : OK

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
tarbo
Offline
Joined: 2006-12-18

You have an intermittent declaration that messes up the syntax.

[code]public String getaCatagoryByName()

String aCatagoryByName=""; // <-- The culprit

{
if (aCatagoryByNumber== 0) aCatagoryByName="Young";
else if (aCatagoryByNumber == 8) aCatagoryByName="Middle";
else if (aCatagoryByNumber == 16) aCatagoryByName="Mature";

return aCatagoryByName;
}[/code]

To prevent this kind of mistakes, consider using either an IDE (such as NetBeans, Eclipse, JBuilder... even MatLab matches braces for you) or putting your opening braces on the same line as the opening block:

[code]
public void functionName(int arg1, int arg2) {
// statements here
}
[/code]

safarirover
Offline
Joined: 2007-06-28

Thanks. I had the statement inside the { and it created somehting like 25 errors, where outside I only received one.

I went through and corrected several syntex mistakes and now I receive no errors.

Here is another possibly stupid question.

On the new code I deleted the setters. I assumed that becuase I get something then the bit must then be set.

For example I will be given a gpa in the test program, once I get it and convert it from a int to a String must the new data be set?

tarbo
Offline
Joined: 2006-12-18

I'm not entirely sure what you're asking here, but I'll try to be both brief as helpful.

Getters and setters are really just [i]wrappers[/i]. From a straightforward point of view, the following have equivalent functionality:

[code]class StructLike {
public String str;
public int iVal;
public Object foo;
}

class Encapsulated {
private String str;
private int iVal;
private Object foo;

public String getStr() { return str; }
public int getIVal() { return iVal; }
public Object getFoo() { return foo; }

public void setStr(String str) { this.str = str; }
public void setIVal(int iVal) { this.iVal = iVal; }
public void setFoo(Object foo) { this.foo = foo; }
}[/code]

The "correct" way would be the second, more verbose. But you see that, in this simple case, all that is happening is that assignment and retrieval are wrapped in methods.

Always look at your objects from the outside. A method called "getFoo" will not go about and "get foo"; it offers functionality to other objects that may want to "get foo" from [i]your[/i] object.

Now, there are two ways you can go about your GPA issue here. You can either have two fields, an integer and a string, and update the one as the other is changed, or you can keep only the integer since the string is defined by the integer. Consider the following two:

[code]
class Example1 {
private String strGPA;
private int intGPA;

public void setGPA(int gpa) {
intGPA = gpa;
strGPA = /* whatever this needs to be */;
}

public String getGPAAsString() {
return strGPA;
}
}

class Example2 {
private int intGPA;

public void setGPA(int gpa) {
intGPA = gpa;
}

public String getGPAAsString() {
if (intGPA < ? ) {
return "tier1";
} else if (intGPA < ? ) {
return "tier2";
} else {
return "tier3";
}
}
}[/code]

Which you pick is your choice. My personal preference goes to the latter (cfr. data orthogonality), but the former is perfectly alright.