So I get an error that says "no suitable constructor found for Student(no arguments constructor)"

/**
 * Write a description of class Student here.
 *
 * @author (your name)
 * @version (a version number or a date)
 */
public class Student
{
    private String name ; 
    private int test1 ; 
    private int test2 ;
    private int test3 ; 

    public Student(String nm, int t1, int t2, int t3)

    {
        name = ""; 
        test1 = 0; 
        test2 = 0; 
        test3 = 0;
    }

    public Student(Student s){
        this(s.name, s.test1, s.test2, s.test3);

    }

    public void setName (String nm)
    { // Set a student's name
        name = nm;
    } 

    public String getName ()
    { // Get a student's name
        return name;
    }

    public void setScore (int i, int score){
        // Set test i to score
        if (i==1) test1 = score;  
        else if (i == 2) test2 = score;
        else   test3 =  score; 

    }

    public int getScore(int i ){
        if (i==1) return test1 ; 
        else if (i==2) return test2 ; 
        else return test3 ; 
    }

    public int getAverage(){
        int average ; 
        average = (int) Math.round((test1 + test2 + test3) / 3.0) ;
        return average ; 
    }

    public int getHighscore(){
        int highScore ; 
        highScore = test1 ; 
        highScore = test1 ; 
        if(test2> highScore) highScore = test2 ; 
        if(test3> highScore) highScore = test3 ; 
        return highScore ;
    }

    public String toString(){
        String str ; 
        str = "Name:    " + "\n" +
        "Test 1:  " + test1 + "\n" + 
        "Test 2:  " + test2 + "\n" +
        "Test 3:  " + test3 + "\n" +
        "Average : "  + getAverage() ; 
        return str ; 
    }
}

import java.util.Scanner ; 
public class StudentApp
{  
    public static void main(String []args)
    {
        Student student1 = new Student() ; 
        Student student2 = new Student() ;                           
        Scanner reader =  new Scanner(System.in) ; 
        String name ; 
        int score ; 
        System.out.println("Enter the first student's score: ") ; 
        name = reader.nextLine() ; 
        student1.setName(name) ; 
        int average ;

        for(int i =1; i<=3 ; i++)
        {
            System.out.print("Enter the student's name: ") ;
            score = reader.nextInt() ;
            student1.setScore(i, score) ; 

        }
        reader.nextLine() ; 
        System.out.print("Enter the second student's name: ") ; 
        student2.setName(name) ;
        for(int i =1 ;  i<=3 ; i++)
        {
            System.out.print("Enter the student's name: ") ;
            score = reader.nextInt() ;
            student1.setScore(i, score) ; 

        }

        System.out.println(student1) ; 
        System.out.println(student2) ; 
        if (student1.getHighscore() > student2.getHighscore())
        {
            name = student1.getName (); 
            score = student2.getAverage() ; 

        }
        else

        {
            name = student2.getName()  ;  
            score = student2.getAverage() ; 
        }
        System.out.println(name + " has the highest average score: " + score) ; 
    }
}

Recommended Answers

All 3 Replies

Here:

Student student1 = new Student() ;

The program is trying to call the default constructor, but the Student class doesn't have one, which, if I'm not mistaken, became required as soon as you added the other constructors. Try adding this:

public Student()
{
    name = ""; 
    test1 = 0; 
    test2 = 0; 
    test3 = 0;
}

Also the initializer constructor, public Student(String nm, int t1, int t2, int t3), is setting everything to default values, instead of using the parameters' values.

Just for the record, a default constructor is not normally required... typically when you create a constructor with args then you deliberately do not have a default constructor because you do not want an instance with missing values. I guess this is one of those cases.

The one place where it is required is where you also have a subclass whose constructor does not start with a call to a superclass constructor
In that case the compiler inserts a call to the superclass default constructor and throws a tantrum if there isn’t one.

Sorry I probably been should have been more verbose. It's my understanding that when there are no constructors defined a default constructor is automatically defined by the compiler.

But if other constructors are defined, then a default constructor isn't automatically defined and is required to be explicitly defined, when you try to access it with a no parameter call in a new statement.

commented: That’s right +14
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.