Download Lab 5

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Lab 5



In this brief lab session, we want to develop a simple menu-driven CLI (Command Line
Interface) program that manages Course data.
The code you practice today will be the basis for the Assignment 6.
Do not submit the programs (practice in the class).
Task 1: Write the Course.java as shown below.
public class Course {
private String name;
private String[] students = new String[2];
private int numberOfStudents;
public Course(String name) {
this.name = name;
}
public String getName(){
return name;
}
public void setName(String name){
this.name = name;
}
public void addStudent(String student) {
if (numberOfStudents >= students.length) {
String[] temp = new String[students.length * 2];
System.arraycopy(students, 0, temp, 0, students.length);
students = temp;
}
students[numberOfStudents++] = student;
}
public String[] getStudents() {
return students;
}
public int getNumberOfStudents() {
return numberOfStudents;
}
/** Clear the course */
1
public void clear() {
numberOfStudents = 0;
}
/** Remove a student from the course */
public void dropStudent(String student) {
for (int i = 0; i < numberOfStudents; i++) {
if (students[i].equalsIgnoreCase(student)) {
// Move students[i + 1] to students[i], etc.
for (int k = i + 1; k < numberOfStudents; k++) {
students[k - 1] = students[k];
}
numberOfStudents--;
break;
}
}
}
}
Task 2: Write menu-driven CLI application based on the following code skeleton.
TestCourse.java
import java.util.Scanner;
public class TestCourse {
public static void main(String[] args) {
__________________//create an array of Course objects named courses (size 2)
__________________//create Scanner object
//create a Course object by Course(“Programming MethosdII”) and assign the object to
//courses[0].
//Repeat the above step for creating another Course object “Operating System” and assign
//the object to courses[1].
//add three students “Peter Jones” “Brian Smith” “Anne Kennedy” to courses[0] by invoking
//addStudent() method
//add two students “Peter Jones” “Steve Smith” to courses[1].
int id; //course id (either 0 or 1)
String studentName;
while(true){// outer while
2
System.out.println("Enter Course ID");
id = kb.nextInt();
if(id <0 || id >1){
______//write a message to the user entered id is invalid
_______// go back to the outer while
}
while(true) {//inner while: handling user choice
int choice = ___________//invoke getChoice method with Scanner object
as an argument
switch(choice){
case 1:
System.out.println("The name of the course id " + id + ": " +
courses[id].getName());
break;
case 2:
getStudentsInfo(courses[id], id);
break;
case 3:
//get the name of the student to be dropped from the course
//drop the student by invoking dropStudent()
//show students enrolled in the course by invoking getStudent()
//break out of switch
case 4:
System.out.println("Remove all the students from the course");
_____________//invoke clear() method
break;
case 5:
System.exit(0);
}//of case
}
}//end of outer while
}// of main
private _________ ____ getChoice(Scanner kb){//complete method signature
int choice;
while(true){
System.out.println("\n\nMain Menu");
System.out.println("1: get course name");
System.out.println("2: get number students taking the course and their
names");
3
System.out.println("3: Drop a student from the course");
System.out.println("4: Reset the course: clear all students records");
System.out.println("5: Exit the application\n\n");
choice = kb.nextInt();
if(choice <1 || choice > 5)
System.out.println("Wrong choice... Try again...");
else break;
}// end of while
return choice;
}//end of getChoice
private static void getStudentsInfo(Course course, int id){
//print out number of students enrolled in the course
//print out the names of the students enrolled in the course
//Do these by invoking proper methods in the base class (i.e., Course class)
//Use for statement to iteratively print out names of the students
}//end of getStudentInfo
}// end of class
4