Results 1 to 18 of 18
  1. #1
    Join Date
    Apr 2011
    Posts
    23
    Rep Power
    0

    Default 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;
    }
    }
    }

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    Quote Originally Posted by program junkie View Post
    Im getting errors
    Do you think that including the error messages in your post would be a good idea? Or should we just guess?

  3. #3
    Join Date
    Apr 2011
    Posts
    23
    Rep Power
    0

    Default

    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

  4. #4
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    Java 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);
    // 
    //
    What on earth is this supposed to be? Go back and read about enums.
    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.

  5. #5
    Join Date
    Apr 2011
    Posts
    23
    Rep Power
    0

    Default

    I'm really not getting it

    thanks for taking out the time

  6. #6
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    Not getting what?

    A. Fix your enum.
    B. Don't assign values to a final variable (other than the initial value).

  7. #7
    Join Date
    Apr 2011
    Posts
    23
    Rep Power
    0

    Default program

    can you please fix this for me

  8. #8
    emschorsch is offline Member
    Join Date
    Apr 2011
    Posts
    6
    Rep Power
    0

    Default

    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.

  9. #9
    Join Date
    Apr 2011
    Posts
    23
    Rep Power
    0

    Default

    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

  10. #10
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    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.

  11. #11
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    Quote Originally Posted by program junkie View Post
    can you please fix this for me
    Quote Originally Posted by program junkie View Post
    I don't want anyone to do this for me.
    Make up your mind!

  12. #12
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default

    Quote Originally Posted by program junkie View Post
    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

    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;
    remove the final keyword and this will be fine.

  13. #13
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default

    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.

  14. #14
    Join Date
    Apr 2011
    Posts
    23
    Rep Power
    0

    Default 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)

  15. #15
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default

    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
    }

  16. #16
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,535
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by ozzyman View Post
    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
    }
    So that level would be an excellent candidate for an Enum; you'd get rid of all those clumsy if-else if-else seqences too.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  17. #17
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default

    its true what josah says, i almost suggested an enum myself but its such a simple script so i didn't bother

  18. #18
    radday55 is offline Member
    Join Date
    Apr 2012
    Posts
    1
    Rep Power
    0

    Default 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

  1. Replies: 2
    Last Post: 04-02-2011, 12:57 PM
  2. Replies: 13
    Last Post: 03-22-2011, 06:49 AM
  3. changing my program to array working program
    By Chewart in forum New To Java
    Replies: 39
    Last Post: 11-18-2009, 06:53 PM
  4. Replies: 0
    Last Post: 04-04-2008, 02:40 PM
  5. Replies: 0
    Last Post: 10-04-2007, 09:33 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •