Results 1 to 18 of 18
Thread: I need help with this program
- 04-18-2011, 04:24 AM #1
Member
- Join Date
- Apr 2011
- Posts
- 23
- Rep Power
- 0
I need help with this program
I have two programs, student client.java and student.java
student.java class should encapsulate the concept of a student, assuming the student has the following attributes: a name and age. Include a constructor, the accessors and mutators, and methods fullString and typeOfStudent.
Im getting errors and don't what I'm doing wrong. can someone please assist me?
Here's the studentclient.java program
public class StudentClient {
public static void main(String[] args) {
Student student1 = new Student("Bob", 15);
Student student2 = new Student("Jan", 13);
System.out.println("Name: " + student1.getName());
System.out.println("Age: " + student1.getAge());
System.out.println("Type of Student: " + student1.typeOfStudent());
System.out.println("\n" + student2.fullString());
System.out.println("Type of Student: " + student2.typeOfStudent());
student1.setName("Ted");
student1.setAge(35);
System.out.println("\n" + student1.fullString());
System.out.println("Type of Student: " + student1.typeOfStudent());
} //ends main
} //ends program
This is what I have so far using the student.java file
import java.util.*;
public class Student{
private String name;
private int age;
public Student (String newName, int newAge){
setName (newName);
setAge (newAge);
}
public void setName (String newName){
name = newName;
}
public String getName(){
return name;
}
public void setAge (int newAge){
if (newAge > 0)
age = newAge;
else
System.out.println ("Age cannot be zero or less.");
}
public int getAge(){
return age;
}
public String fullString (){
return ("\nName of Student:" + name+ "\nAge of Student:" +age+ "Years.");
}
public String TypeOfStudent(){
for (TypeOfStudent t : TypeOfStudent.values()){
if (age >= t.getMinAge() && age <= t.getMaxAge())
return t.getName();
}
}
public enum TypeOfStudent {PRESCHOOL(0, 4), KINDERGARTEN(5, 5), ELEMENTAR SCHOOL(6, 10), MIDDL SCHOOL(11, 13), HIGH SCHOOL(14, 17), COLLEGE(18, 99);
private final int minAge, maxAge;
private final String name;
public String TypeOfStudent (int minAge, int maxAge, String name){
this.minAge = minAge;
this.maxAge = maxAge;
this.name = name;
}
public int getMinAge(){
return minAge;
}
public int getMaxAge(){
return maxAge;
}
public String getName(){
return name;
}
}
}
- 04-18-2011, 04:33 AM #2
- 04-18-2011, 04:45 AM #3
Member
- Join Date
- Apr 2011
- Posts
- 23
- Rep Power
- 0
Sorry very tired
error message are:
missing return statement line 44
cannot find symbol symbol constructor TypeOfStudent (int,int)
location: class student.TypeOfStudent line 51
line 57 missing return statement
line 59 cannot assign a value to final variable minAge
line 60 cannot assign a value to final variable maxAge
line 61 cannot assign a value to final variable name
Thanks
- 04-18-2011, 04:51 AM #4Java Code:
public enum TypeOfStudent {PRESCHOOL(0, 4), KINDERGARTEN(5, 5), ELEMENTAR SCHOOL(6, 10), MIDDL SCHOOL(11, 13), HIGH SCHOOL(14, 17), COLLEGE(18, 99); // //
Having an enum and 2 methods with the same name only leads to confusion.
As for the other three errors, surely they are straightforward! You are trying to assign a value to a final variable. You cannot do that. The whole point of making them final is so you cannot change their value.
- 04-18-2011, 04:59 AM #5
Member
- Join Date
- Apr 2011
- Posts
- 23
- Rep Power
- 0
I'm really not getting it
thanks for taking out the time
- 04-18-2011, 05:02 AM #6
Not getting what?
A. Fix your enum.
B. Don't assign values to a final variable (other than the initial value).
- 04-18-2011, 06:01 AM #7
Member
- Join Date
- Apr 2011
- Posts
- 23
- Rep Power
- 0
program
can you please fix this for me
- 04-18-2011, 06:06 AM #8
Member
- Join Date
- Apr 2011
- Posts
- 6
- Rep Power
- 0
yo program junkie I'll give you a hint they (people who know what they're doing) hate it when you ask them to just fix it for you.
- 04-18-2011, 06:10 AM #9
Member
- Join Date
- Apr 2011
- Posts
- 23
- Rep Power
- 0
I don't want anyone to do this for me. I got frustrated. I do need some guidance with the enum if you could help
-
You should read the enum tutorial at the Oracle Java tutorials. If your enum passes parameters, you'll need to create a constructor for the enum. Your enum doesn't have one, but rather has a "pseudo"-constructor -- a method that looks sort of like a constructor (but takes three parameters!), but isn't since it has a void return type. Remember that constructors have no return type.
- 04-18-2011, 06:54 AM #11
-
When you get error messages like these, always follow the line number the compiler gives you. The first one is at line 57, which points here:
Java Code:public [COLOR="Red"]String[/COLOR] TypeOfStudent (int minAge, int maxAge, String name){ this.minAge = minAge; this.maxAge = maxAge; this.name = name; }
As you can see, you have defined a method with the return type String. Which means that the method should return a String like by putting this at the end: return name;. But judging by your method, it looks like you don't need to it send any String back so the return type should be void instead, and then you wont need a return statement at all.
This is the fix:
Java Code:public [COLOR="red"]void[/COLOR] TypeOfStudent...
The other 3 lines also point to this same method. It turns out that you have declared minAge, maxAge, and name already, so they can't be change since you have used the final keyword when you declared them. When something is declared 'final' it means once it is set, it is not allowed to be changed at all.
Java Code:private [COLOR="Red"]final[/COLOR] int minAge, maxAge; private [COLOR="red"]final[/COLOR] String name;
-
And i just realised everything was encapsulated in the enum class. I don't think this should be an enum - enums are Constants and should only be used for information that was known at compile time. When you're writing a script for a class of Students - obviously the students change over time, their ages change, their names might change, so you should remove the enum keyword and turn it into a normal class, then create instances of the class with the data you have already.
- 04-18-2011, 03:23 PM #14
Member
- Join Date
- Apr 2011
- Posts
- 23
- Rep Power
- 0
New to programming need help
Thanks Ozzyman
I'm new to programming and one thing has me stumped.
Creating the typeOfStudent method that returns the most likely level of schooling for the student
preschool (age 0-4)
kindergarten (age 5)
middle School ( age 11-13)
-
Start with a blank method, then add whatever you know to it
Java Code://method to take a students age and return the schooling level public String getSchoolLevel(int studentAge) { //define a string to return String schoolingLevel = ""; //return a string describing the level return schoolingLevel; }
By looking at the above, its clearer to see that all you need is a few conditional statements to return the correct schooling level depending on the age.
if (studentAge < 5) {
schoolingLevel = "Pre-School";
} else if (studentAge == 5) {
schoolingLevel = "Kindergarten";
} else if (studentAge >= 11 && studentAge <= 13) {
schoolingLevel = "Middle School";
} else {
//decide what to do if age is out of range
}
- 04-19-2011, 12:15 PM #16
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 14,422
- Blog Entries
- 7
- Rep Power
- 28
-
its true what josah says, i almost suggested an enum myself but its such a simple script so i didn't bother
- 04-25-2012, 01:23 AM #18
Member
- Join Date
- Apr 2012
- Posts
- 1
- Rep Power
- 0
Re: I need help with this program
public class Student {
private String name;
private int age;
public Student(String newName, int newAge)
{
setName (newName);
setAge(newAge);
}
public void setName (String newName){
name = newName;
}
public String getName (){
return name;
}
public void setAge (int newAge){
if (newAge > 0)
age = newAge;
else
System.out.println ("Age cannot be zero or less.");
}
public int getAge ()
{
return age;
}
public String fullString ()
{
return ("\nName of Student: " + name+ "\nAge of Student: " + age + " Years old ");
}
//this give the students placement in what type of school.
public String typeOfStudent()
{
if ((age >= 0) && (age <= 4))
{
return "Preschool";
}
else if((age >=5) && (age <=5))
{
return "Kindergarten";
}
else if((age >= 6) && (age <=10))
{
return "Elementary School";
}
else if ((age >=11) && (age <=13))
{
return "Middle school";
}
else if ((age >= 14) && (age <= 17))
{
return "High School";
}
else if ((age >=17) && (age <=100))
{
return "College";
}
else
{
return "opps";
}
}
This worked for me.
Similar Threads
-
How to code a program to send messages to a chat program?
By josh2992 in forum New To JavaReplies: 2Last Post: 04-02-2011, 01:57 PM -
How would I open a program from a single button of another program. Help...
By decgaid06 in forum New To JavaReplies: 13Last Post: 03-22-2011, 07:49 AM -
changing my program to array working program
By Chewart in forum New To JavaReplies: 39Last Post: 11-18-2009, 07:53 PM -
How to execute an External Program through Java program
By Java Tip in forum java.ioReplies: 0Last Post: 04-04-2008, 03:40 PM -
How to execute an External Program through Java program
By JavaBean in forum Java TipReplies: 0Last Post: 10-04-2007, 10:33 PM
Bookmarks