im working on this program and im getting really stuck. i was hoping to get it done soon, but it is dragging out. i have completed the first half of the instructions, i believe.

2. Create an abstract class called Student. 
a. Student will need instance variables name , qz1, qz2, qz3, qz4, and 
qz5 (of types String and int, respectively). 
b. Student will need appropriate methods and constructors. To make things 
interesting, create a getQuiz() method that takes in a quiz number as input and 
then returns the appropriate quiz value. Likewise, setQuiz() will take as input 
a quiz number and quiz score, and then put the value into the right variable. Make 
sure to have a toString() method that prints the name of the Student along 
with the quiz scores. 
c. Save the class as Student.java.

im going about doing this with this code.

/* Student.java
Manage a student's name and five test scores.
*/
public class Student {

   //Instance variables
   //Each student object has a name and three test scores
   private String name;             //Student name
   private int qz1;               //Score on test 1
   private int qz2;               //Score on test 2
   private int qz3;               //Score on test 3
   private int qz4;               //Score on test 4
   private int qz5;               //Score on test 5




   public Student(){
   //Initialize a new student's name to the empty string and the test
   //scores to zero.
      name = "";
      qz1 = 0;
      qz2 = 0;
      qz3 = 0;
      qz4 = 0;
      qz5 = 0;
   }
    
   //Other methods

   public void setName (String nm){
   //Set a student's name
      name = nm;
   }
    
   public String getName (){
      return name;
   }
  
   public void setQuiz (int i, int score){
   //Set test i to score
      if (i == 1) qz1 = score;
      else if (i == 2) qz2 = score;
      else if (i == 3) qz3 = score;
      else if (i == 4) qz4 = score;
      else if (i == 5) qz5 = score;
   }

   public int getQuiz (int i){
   //Retrieve score i
      if (i == 1) return qz1;
      else if (i == 2) return qz2;
      else if (i == 3) return qz3;
      else if (i == 4) return qz4;
      else if (i == 5) return qz5;
      
   }
   

   public String toString(){
   
      String str;
      str = "Name:    " + name  + "\n" +    // "\n" denotes a newline
            "Test 1:  " + qz1 + "\n" +
            "Test 2:  " + qz2 + "\n" + 
            "Test 3:  " + qz3 + "\n" +
            "Test 4:  " + qz4 + "\n" +
            "Test 5:  " + qz5 + "\n" +
            return str;
             
        }
    }
}

like i said, i think this is right.

the trouble comes when i have to go about the second half of the assignment

3. You are to create a class called TestStudent and save it as TestStudent.java. 
a. Make sure that you create an array called myClass. Add the following Students, 
with their quiz scores: 

Candidate Q1 Q2 Q3 Q4 Q5 
Mark Kennedy 70 80 90 100 90 
Max Gerard 80 85 90 85 80 
Jean Smith 50 79 89 99 100 
Betty Farm 85 80 85 88 89 
Dilbert Gamma 70 70 90 70 80 

b. Create a method called printBook() that traverses through the array and prints 
out each element. 
c. Create a method called replaceName() that replaces a Student’s name with a 
new one. 
d. Create a method called replaceQuiz() that replaces a Student’s quiz grade 
with a new one. It should replace only one quiz grade as indicated when it is 
called. It will have the array, quiz number, and quiz value as input. 
e. Create a method called replaceStudent() that replaces a Student with 
another one. It will have the array, name to replace, new Student name, and quiz 
scores as input. 
f. Create a method called insertStudent() that inserts a new Student before 
another Student in the array. It will have the array, name to find, new Student 
name, and quiz scores as input. 
g. Create a method called deleteStudent() that finds a Student by name and 
then deletes that Student.

im not really familiar with sorting and it is confusing me horribly. as i was just looking i realized realized that i need to make my qz an array list. im going to get to that. but if anyone has any help to offer it is appreciated.

Recommended Answers

All 15 Replies

i have been working for a few hours and have been running into alot of problems. if anyone has any advice it is very much appreciated.

The spec doesn't say anything about making an "array list". It says to make an array.

Make sure that you create an array called myClass.

Java has plain old arrays and it has Collections, one of which is an ArrayList.

http://www.j2ee.me/javase/6/docs/api/java/util/ArrayList.html

Find out for sure whether you are to use an array or an ArrayList or if the choice is yours. If possible, use an ArrayList as opposed than an array. ArrayList does a lot of stuff for you that you that you'd have to do on your own with a regular old array.

So before you can do anything, you need to answer the array versus ArrayList question. My reading of the spec suggests that you are to use an array, not an ArrayList.

Regardless, ignore all sorting, searching, and deleting for now. Just have an "add element" function and a "display" function to start with, then tackle the others. Your first task is to set up the actual class itself with its constructor and a call from main.

i think i am definitely going to go with the arraylist. i actually have some of the class already built, but i thought it may be insignificant to post it. im going to work on it some more to see how far i can get. if anyone has advice etc im very open.

where does it say you have to sort anything?

alright, i have been working on this pretty hard. and the sorting thing was a misconception on my part. i have most of the needed information and methods, just the methods are not well adapted to fit the context of the program as i am getting them from examples and dont really know how to make them fit perfectly. the code is a huge jump from my previous post. at that time i hadnt really looked everything over to well and i asked premature questions. well here is my code as of now.

package assignment;

public class Student {
    String StudentName;
    int qz1;
    int qz2;
    int qz3;
    int qz4;
    int qz5;

    public Student (String name, int q1, int q2, int q3, int q4, int q5)
    {
        StudentName = name;
        qz1 = q1;
        qz2 = q2;
        qz3 = q3;
        qz4 = q4;
        qz5 = q5;
    }
    
    public static void getQuiz()
    {
        
        
    }
    
    public static void setQuiz()
    {
        
        
    }

    public static void toString(String name, int q1,
            int q2, int q3, int q4, int q5)
    {
        System.out.println("Name" + "Q1" + "Q2" + "Q3" + "Q4" + "Q5");
        System.out.println(name + q1 + q2 + q3 + q4 + q5);
    }

}
package assignment;

public class StudentTester {
    
    public static void main (String [] args)
    {

        Student[] kids = new Student[5];
        kids[0] = new Student("Mark", 70, 80 ,90, 100, 90);
        kids[1] = new Student("Max", 80, 85, 90, 85, 80);
        kids[2] = new Student ("Jean", 50, 79, 89, 99, 100);
        kids[3] = new Student ("Betty", 85, 80, 85, 88, 89);
        kids[4] = new Student ("Dilbert", 70, 70, 90, 70, 80);


        public static void printBook(kids[] list)
        {
            for(int i = 0; i < list.length; i++)
            System.out.println(list[i]);
        }

        public static void replaceName(kids[] list,
        String find, String replace)
        {
            for(int index = 0; index < list.length; index++)
                if (list[index].getName().equals(find))
                    list[index].setName(replace);
        }

        public static void replaceQuiz(kids[] list,
        String find, String replace)
        {
            for(int index = 0; index < list.length; index++)
                if (list[index].getName().equals(find))
                    list[index].setName(replace);
        }

        public static void replaceStudent(kids[] list,
        String find, String replace)
        {
            for(int index = 0; index < list.length; index++)
                if (list[index].getName().equals(find))
                    list[index].setName(replace);
        }


        public static void insertStudent(kids[] list,
        int location, String addN, int addS)
        {
            for(int index = list.length - 1; index > location; index--)
            list[index] = list[index-1];
            list[location] = new kids(addN, addS);
        }

        public static void deleteByName(kids[] list,
        String find)
        {
            int location = 0;
            int index;
            // find location of item you want to delete
            for(index = 0; index < list.length; index++)
            if ((list[index] != null) &&
            (list[index].getName().equals(find)))
            {
                location = index;
                break;
            }
            else if (list[index] == null)
            {
                location = -1;
                break;
            }
            if ((index != list.length) && (location >= 0))
            { //move items up in the array
                for(index = location; index < list.length -1; index++)
                list[index] = list[index + 1];
                list[list.length-1] = null;
            }
        }
}

for the two methods in the student class, get and set quiz, i have left them blank as i dont really have any good ideas of what is supposed to be done there. as i have said before all my methods in my tester class root from examples. im pretty sure that there are some dumb mistakes there. if anyone can help me fix my methods so that they apply to my current situation i would be very grateful. thanks in advance.

Generally, toString() will not take any parameters. Use the object's data members (StudentName, qz1, qz2, qz3, qz4, qz5) and display them. I cannot think of any reason you'd want to make this a static function. Have you been told to make it a static function? If not, don't. Similarly getQuiz () should not be static and I can't think of any way that setQuiz () would be static. If there's an object involved, there's no reason to make it static. If there isn't an object involved, there's nothing to "set" or "get".

Unless there is a reason, make all of your functions non-static. get functions almost always return something. set functions almost always are void and take some value to be set. In addition, I imagine that if you are setting or getting a single quiz, you should also pass the quiz number to the functions (in the range of 1 through 5).

An example of get and set functions might be:

public int getQuiz (int quizNumber)
{
    // return qz1, qz2, qz3, qz4, or qz5 depending on quizNumber
}

public void setQuiz (int quizNumber, int score)
{
    // set qz1, qz2, qz3, qz4, or qz5 to score  depending on quizNumber
}

I second VernonDozier's reply for the get and set method.

as far as setting the methods as you have described in your comments, im confused about what i am exactly supposed to do in terms of code there. that was most of the reason why i didnt have the methods doing anything yet.

as far as setting the methods as you have described in your comments, im confused about what i am exactly supposed to do in terms of code there. that was most of the reason why i didnt have the methods doing anything yet.

I think a switch statement would be appropriate here:

public int getQuiz (int quizNumber)
{
    switch (quizNumber)
    {
        case 1: return qz1; break;
        case 2: return qz2; break;
        case 3: return qz3; break;
        case 4: return qz4; break;
        case 5: return qz5; break;
    }
    return -99;  // illegal parameter
}

public void setQuiz (int quizNumber, int score)
{
    // set qz1, qz2, qz3, qz4, or qz5 to score  depending on quizNumber
    switch (quizNumber)
    {
        case 1: qz1 = score; break;
        case 2: qz2 = score; break;
        case 3: qz3 = score; break;
        case 4: qz4 = score; break;
        case 5: qz5 = score; break;
    }
}

Didn't mean to write the whole thing for you, but you still have plenty to do!

im getting an error that says that each of the cases is an unreachable statement, any ideas?

im getting an error that says that each of the cases is an unreachable statement, any ideas?

Whoops! My bad! I forgot all about the fact that Java doesn't allow unreachable statements. I knew that the "break" commands would never execute, but I got in the habit of sticking them in anyway. It's not the cases that are unreachable, it's the "break" statements. Delete the five "break" statements in the getQuiz (int) function. Keep them in the setQuiz (int, int) function.

Sorry about that! That's what I get for submitting without actually compiling!

package assignment;

public class StudentTester {
    
    public static void main (String [] args)
    {

        Student[] kids = new Student[5];
        kids[0] = new Student("Mark", 70, 80 ,90, 100, 90);
        kids[1] = new Student("Max", 80, 85, 90, 85, 80);
        kids[2] = new Student ("Jean", 50, 79, 89, 99, 100);
        kids[3] = new Student ("Betty", 85, 80, 85, 88, 89);
        kids[4] = new Student ("Dilbert", 70, 70, 90, 70, 80);


        public static void printBook(Student[] list)
        {
            for(int i = 0; i < list.length; i++)
            System.out.println(list[i]);
        }

        public static void replaceName(Student[] list,
        String find, String replace)
        {
            for(int index = 0; index < list.length; index++)
                if (list[index].getName().equals(find))
                {
                    list[index].setName(replace);
                }
        }

        public static void replaceQuiz(Student[] list,
        String find, String replace)
        {
            for(int index = 0; index < list.length; index++)
                if (list[index].getName().equals(find))
                {
                    list[index].setName(replace);
                }
        }

        public static void replaceStudent(Student[] list,
        String find, String replace)
        {
            for(int index = 0; index < list.length; index++)
            {
                if (list[index].getName().equals(find))
                    list[index].setName(replace);
            }
        }


        public static void insertStudent(Student[] list,
        int location, String addN, int addS)
        {
            for(int index = list.length - 1; index > location; index--)
            {
                list[index] = list[index-1];
                list[location] = new Student(addN, addS);
            }
        }

        public static void deleteByName(Student[] list,
        String find)
        {
            int location = 0;
            int index;
            // find location of item you want to delete
            for(index = 0; index < list.length; index++)
            {
                if ((list[index] != null) &&
                (list[index].getName().equals(find)))
                {
                    location = index;
                    break;
                }
                else if (list[index] == null)
                {
                    location = -1;
                    break;
                }
                if ((index != list.length) && (location >= 0))
                { //move items up in the array
                    for(index = location; index < list.length -1; index++)
                    {
                        list[index] = list[index + 1];
                        list[list.length-1] = null;
                    }
                }
            }
        }
    }

i have gotten most of my student class straighted out, but im having alot of trouble with my tester class. i know that it is on the right track because i got most of the code from examples that were intended to be used with the program. im having trouble getting the everything to work together. i know that it is on the right track, but im not sure how exactly to get it to work...

if anyone can help me sort it out or present a solution i would be very grateful. this is the hardest thing i have done to date and it is starting to become overwhelming.

There's a key word in your description in post 1 that really affects everything. It says that Student should be an "abstract" class. It's not clear to me why. Is there something else to the spec that you haven't posted? Have you covered abstract classes in class? There must be some class that extends Student somewhere?

I should have brought up the "abstract" modifier much earlier as it throws a monkey wrench into the whole assignment. I'm not sure what you're supposed to do and why.

i attached a pdf of the official instructions. it is a distance learning class, i think we skipped over it, but were supposed to glance over it sometime. im honestly not sure what it is. i know that it extends a class though. i have been working on the methods and trying to get those sorted out. i tried to take out all of the stuff that didnt work and just do what i could figure out for now. right now im at this point:

package assignment;
import java.util.*;

public class StudentTester {
    
    public static void main (String [] args)
    {


       ArrayList<Student> kids = new ArrayList<Student>();
        kids.add(new Student("Mark", 70, 80 ,90, 100, 90));
        kids.add(new Student("Max", 80, 85, 90, 85, 80));
        kids.add(new Student ("Jean", 50, 79, 89, 99, 100));
        kids.add(new Student ("Betty", 85, 80, 85, 88, 89));
        kids.add(new Student ("Dilbert", 70, 70, 90, 70, 80));


        public static void printBook()
    {
        
    }

        public static void replaceName()
        {
            
        }

        public static void replaceQuiz()
        {
            
        }

        public static void replaceStudent()
        {
            
        }


        public static void insertStudent(Student[] list, 
                                    int location, String addN, int addG)
        {
            for(int index = list.length - 1; index > location; index--)
            list[index] = list[index-1];
       
            list[location] = new Student(addN, addG);
        }
            
        
        public static void deleteStudent(Student[] list, 
                                    String find)
        {
            
        }
}

im trying to implement this now for the delete method. im really winging this and figuring it out as i go. im trying to follow examples, but they im not able to follow at some points.

i attached a pdf of the official instructions. it is a distance learning class, i think we skipped over it, but were supposed to glance over it sometime. im honestly not sure what it is. i know that it extends a class though.

Stop what you are doing and read the instructions several times. That's always the first thing. You can't do anything until then. When you read them, you'll see that you are not following them. You're supposed to have an array, not an ArrayList in TestStudent. You have an ArrayList in TestStudent2. There's also at least one more instruction you did not follow.

i know that it extends a class though.

I see nothing in the pdf about any class extending any other class.

i have been working on the methods and trying to get those sorted out. i tried to take out all of the stuff that didnt work and just do what i could figure out for now. right now im at this point:

im trying to implement this now for the delete method. im really winging this and figuring it out as i go. im trying to follow examples, but they im not able to follow at some points.

This doesn't make sense to me. I assume you have been compiling this as you go? If so, you realize that it won't compile due to a brackets mismatch problem? That's completely independent of the functions you mention here, so you need to solve that first.


As far as the Student class being abstract goes, as mentioned, that's going to screw a lot of things up because once you declare Student an abstract class, if you keep everything else the same, a bunch of errors are going to pop up. If Student is to be abstract, you need to find out why and it's going to need some subclasses. I see nothing in the pdf explaining why Student is abstract or what extends it.

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.