I'm having a problem with constructor. I get an error an error "invalid method declaration; return type required" on line 75 ..here is line 75 TypeOfStudent(int minAge, int maxAge, String name) ..and I've posted my whole code below

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), ELEMENTARY_SCHOOL(6, 10), MIDDLE_SCHOOL(11, 13), HIGH_SCHOOL(14, 17), COLLEGE(18, 99)}
    
    
    private final int minAge, maxAge;
    private final String name;
    
   
    
    
     
     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;
       }
 }
}
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

 

}

Recommended Answers

All 3 Replies

Exctly as it says. Compare line 75 with line 54. What's missing at the front of the line 75 statement?

Well after fixing that I got a ton of new errors now....
cannot assign a value to final variable name
[cannot find symbol method getMinAge()..
cannot find symbol method getMaxAge()..line 57
cannot find symbol method getName()..line 58
cannot find symbol constructor TypeOfStudent(int,int)..line 64
cannot assign a value to final variable minAge
cannot assign a value to final variable maxAge
cannot assign a value to final variable name

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), ELEMENTARY_SCHOOL(6, 10), MIDDLE_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;
       }
 }

Because that brace after the list of values for the enum ends the enum. Remove it and replace it with a semi colon.

Then the second "TypeOfStudent "method" is actually a constructor, and since it is an enum constructor it needs to be private, so change "public String" to "private" for that constructor.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.