Hello everyone, I quite new at JAVA and one assignment is on my nose and I am going crazy..
My task is: "Create a solution for the following scenario: A local community mobile college unit holds 2 different classes in literacy and numeracy for mature students. A student can join the mobile unit and sign up to one or both classes. The students ID number, name, age and telephone no should be held on the system for initial sign up." and "An ultimate solution would be where the two courses can be set up and then students enrolled on the courses. A menu system should allow for input of extra courses (in case of expansion) input of student details and students to enroll on courses.
The menu should also allow a list of students by course, a list of all students by age.
".

Maybe I am doing it the wrong way, but..
I have created the WHILE for number of students to enroll on specific courses and my friend helped me to array it.. but I cant figure out how to print.out the information and if the array is where it supposed to be.

1st file:

import java.util.Scanner;

class test1 
 { 
	static student mystudent[] = new student[30];  // - create the array (30 element will be more then enough)
	static int arrindex = 0;			// - item index for the array

 	public static void menu()
 	{
 		System.out.println("\n\n1. Numeracy");
 		System.out.println("2. Literacy");
 		System.out.println("3. Add another course and enroll to it");
 		System.out.println("4. List of students by age");
 		System.out.println("5. List of students by course.");
 		System.out.println("6. Exit");
 	}

	public static void main (String[]args) 
 	{  
 		
 		Scanner input = new Scanner(System.in);
 		
 		String studName, newCourse;
 		int studId, studAge, studTelNr, option=0;
 		
 		System.out.println("Wellcome to mobile college!");
 		
 		while (option!=6)
 		{
 			System.out.println("Choose your course you want to enroll to.");
 				
 			menu();
 			
 			System.out.println("");
 			option = input.nextInt();
 			
 			if (option!=6)
 			{	
 				
 				if (option==1)
 				{
 					System.out.println("\n\nFill in all details to enroll to Numeracy:");
 					System.out.println("\nYour student ID number ");
 					studId = input.nextInt();
 					System.out.println("Your Name ");
 					studName = input.next();
 					System.out.println("Your Age ");
 					studAge = input.nextInt();
 					System.out.println("Your telephone number ");
 					studTelNr = input.nextInt();
 				
 					mystudent[arrindex] = new student (studId, studName, studAge, studTelNr, "Numeracy"); // - put the new student to the array
					arrindex++;								  // - increase itemindex
 				}
 				
 				if (option==2)
 				{
 					System.out.println("Fill in all details to enroll to Literacy:");
 					System.out.println("\nYour student ID number ");
 					studId = input.nextInt();
 					System.out.println("Your Name ");
 					studName = input.next();
 					System.out.println("Your Age ");
 					studAge = input.nextInt();
 					System.out.println("Your telephone number ");
 					studTelNr = input.nextInt();
 				
 					mystudent[arrindex] = new student (studId, studName, studAge, studTelNr, "Literacy");
					arrindex++;
 				}
 		
 				if (option==3)
 				{
 					
 					System.out.println("Please enter the name of the course: ");
 					newCourse = input.next();
 					System.out.println("\nYour student ID number ");
 					studId = input.nextInt();
 					System.out.println("Your Name ");
 					studName = input.next();
 					System.out.println("Your Age ");
 					studAge = input.nextInt();
 					System.out.println("Your telephone number ");
 					studTelNr = input.nextInt();
 				
 					mystudent[arrindex] = new student (studId, studName, studAge, studTelNr, "Literacy");
					arrindex++;
 				}
 				
 				if (option==4)
 				{
 					
 				}
 				
 				if (option==5)
 				{
 					System.out.println(""+student.getAge());
 				}
 			}
 		}	
 	}

	void sortbyage()
		{// - sort the array by age
			student tmp = null;
			int i=0;
			int j=1;
			while(i<=arrindex)
			{
				while(j<=arrindex)
				{
					if(mystudent[i].getAge()>mystudent[j].getAge())
					{
						tmp = mystudent[i];
						mystudent[i]=mystudent[j];
						mystudent[j]=tmp;
					}
					else
					{//if	
						j++;
					}//else
				}//for j		
				i++;
			}//for i
			
		
		}
		
}

2nd file:

/* the pets class*/
 
 
 class student
 {
 	private int id;
 	private String name;
 	private int age;
 	private int number;
 	private String course;
 	
 	student (int i, String n, int a, int nr, String c)
 	{
 		id = i;
 		name = n;
 		age = a;
 		number = nr;
		course = c;
 	}
 	
 	int getId()
 	{
 		return id;
 	}
 	
 	String getName()
 	{
 		return name;
 	}
 	
 	int getAge()
 	{
 		return age;
 	}
 	
 	int getNr()
 	{
 		return number;
 	}
  	String getCourse()
 	{
 		return course;
 	}
}

Any help is very much appreciated!

Recommended Answers

All 6 Replies

The following code has not been tested very well and lacks input validation.
Generics are not used as I am not sure which Java version you have.

import java.util.*;

class Student {
	public String name, phone;
	public int id, age;
	
	public String toString() {
		return "Student " + name + ", id=" + id + ", age=" + age + ", phone=" + phone;
	}
}

class StudentAgeComparator implements Comparator {
	public int compare(Object one, Object two) {
		Student s1 = (Student)one;
		Student s2 = (Student)two;
		return s1.age - s2.age;
	}
	
	public boolean equals(Object other) {
		return this == other;
	}
}

class Course {
	private String name;
	private ArrayList students = new ArrayList();
	
	public Course(String name) {
		this.name = name;
	}
	
	public String getName() {
		return name;
	}
	
	public void addStudent(Student student) {
		students.add(student);
	}
	
	public ArrayList getStudents() {
		return students;
	}
	
	public String toString() {
		return "Course: " + name + " (" + students.size() + " enrolled)";
	}
}

class College {
	private ArrayList courses = new ArrayList();
	private ArrayList students = new ArrayList();
	
	private Scanner input = new Scanner(System.in);
	
	private int readOption() {
		System.out.println("0. Quit");
		System.out.println("1. Add course");
		System.out.println("2. Add student");
		System.out.println("3. Enroll student into course");
		System.out.println("4. List students by age");
		System.out.println("5. List students by course");
		System.out.print("Enter your selection: ");
		return input.nextInt();
	}
	
	private void addCourse() {
		System.out.println("Adding a course...");
		System.out.print("Enter course name: ");
		String name = input.next();
		courses.add(new Course(name));
		System.out.println("Course " + name + " added.");
	}
	
	private void addStudent() {
		System.out.println("Adding a student...");
		Student stud = new Student();
		System.out.print("Student name: ");
		stud.name = input.next();
		System.out.print("Student id: ");
		stud.id = input.nextInt();
		System.out.print("Student age: ");
		stud.age = input.nextInt();
		System.out.print("Student phone: ");
		stud.phone = input.next();
		students.add(stud);
		System.out.println("Student " + stud.name + " added.");
	}
	
	private void enroll() {
		System.out.println("Enrolling student into course...");
		
		System.out.println("Select student:");
		for (int i=0; i < students.size(); ++i) {
			Student s = (Student)students.get(i);
			System.out.println((i+1) + ". " + s.toString());
		}
		int studIndex = input.nextInt() - 1;
		
		System.out.println("Select course:");
		for (int i=0; i < courses.size(); ++i) {
			Course c = (Course)courses.get(i);
			System.out.println((i+1) + ". " + c.toString());
		}
		int courseIndex = input.nextInt() - 1;
		
		Course course = (Course)courses.get(courseIndex);
		Student stud = (Student)students.get(studIndex);
		course.addStudent(stud);
		System.out.println("Student " + stud.name + " enrolled in " + course.getName());
	}
	
	public void listByAge() {
		ArrayList list = new ArrayList(students);
		Collections.sort(list, new StudentAgeComparator());
		System.out.println("Students by age:");
		for (int i=0; i < list.size(); ++i) {
			Student s = (Student)list.get(i);
			System.out.println(s.toString());
		}
	}
	
	public void listByCourse() {
		System.out.println("Students by course:");
		for (int i=0; i < courses.size(); ++i) {
			Course c = (Course)courses.get(i);
			System.out.println(c.toString());
			ArrayList list = c.getStudents();
			for (int j=0; j < list.size(); ++j) {
				Student s = (Student)list.get(j);
				System.out.println("  " + s.toString());
			}
		}
	}
	
	public void operate() {
		int option = 0;
		while ((option = readOption()) > 0) {
			if (option == 1)
				addCourse();
			else if (option == 2)
				addStudent();
			else if (option == 3)
				enroll();
			else if (option == 4)
				listByAge();
			else if (option == 5)
				listByCourse();
			else
				System.out.println("Unknown option.");
		}
		System.out.println("College is closing. Good bye.");
	}
}

public class Main {
	public static void main(String[] args) {
		College college = new College();
		college.operate();
	}
}
commented: very well structured! +0

Waw!
Although compiler doesn't like it very much "c:\*_*\test1.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
Process completed."
BUT it works like a charm! Its just brilliant!
Thank you so much for your help!!! You have saved my day :)

What the compiler doesn't like is the fact that generics are not used.
Like I said in the previous post, "Generics are not used as I am not sure which Java version you have."
But if you change ArrayList declarations from "raw" ArrayLists to generic ones, the warnings should go away.

//ArrayList students = new ArrayList();
ArrayList<Student> students = new ArrayList<Student>();

//ArrayList courses = new ArrayList();
ArrayList<Course> courses = new ArrayList<Course>();

If you recompile the code with the -Xlint:unchecked option, it should tell you all the places where "unchecked" code is used.

What the compiler doesn't like is the fact that generics are not used.
Like I said in the previous post, "Generics are not used as I am not sure which Java version you have."
But if you change ArrayList declarations from "raw" ArrayLists to generic ones, the warnings should go away.

//ArrayList students = new ArrayList();
ArrayList<Student> students = new ArrayList<Student>();

//ArrayList courses = new ArrayList();
ArrayList<Course> courses = new ArrayList<Course>();

If you recompile the code with the -Xlint:unchecked option, it should tell you all the places where "unchecked" code is used.

I am using java-6; jdk-6.20 version.
I have changet these 2 you gave me, but have no idea how to change the rest, I have compiled it with -Xlint:unchecked and it came up with these errors:

"C:\..\test1.java:118: warning: [unchecked] unchecked call to ArrayList(java.util.Collection<? extends E>) as a member of the raw type java.util.ArrayList
ArrayList list = new ArrayList(students);
^
C:\..\test1.java:119: warning: [unchecked] unchecked conversion
found : java.util.ArrayList
required: java.util.List<T>
Collections.sort(list, new StudentAgeComparator());
^
C:\..\test1.java:119: warning: [unchecked] unchecked conversion
found : StudentAgeComparator
required: java.util.Comparator<? super T>
Collections.sort(list, new StudentAgeComparator());
^
C:\..\test1.java:119: warning: [unchecked] unchecked method invocation: <T>sort(java.util.List<T>,java.util.Comparator<? super T>) in java.util.Collections is applied to (java.util.ArrayList,StudentAgeComparator)
Collections.sort(list, new StudentAgeComparator());
^
"

For these lines:

ArrayList list = new ArrayList(students);
		Collections.sort(list, new StudentAgeComparator());

Here is a version of the code updated to use generics.
I commented out the parts of the code that are not needed when using generics--its mostly the type casts.

import java.util.*;

class Student {
	public String name, phone;
	public int id, age;
	
	public String toString() {
		return "Student " + name + ", id=" + id + ", age=" + age + ", phone=" + phone;
	}
}

class StudentAgeComparator implements Comparator<Student> {
	public int compare(Student one, Student two) {
		//Student s1 = (Student)one;
		//Student s2 = (Student)two;
		return one.age - two.age;
	}
	
	public boolean equals(Object other) {
		return this == other;
	}
}

class Course {
	private String name;
	private ArrayList<Student> students = new ArrayList<Student>();
	
	public Course(String name) {
		this.name = name;
	}
	
	public String getName() {
		return name;
	}
	
	public void addStudent(Student student) {
		students.add(student);
	}
	
	public ArrayList<Student> getStudents() {
		return students;
	}
	
	public String toString() {
		return "Course: " + name + " (" + students.size() + " enrolled)";
	}
}

class College {
	private ArrayList<Course> courses = new ArrayList<Course>();
	private ArrayList<Student> students = new ArrayList<Student>();
	
	private Scanner input = new Scanner(System.in);
	
	private int readOption() {
		System.out.println("0. Quit");
		System.out.println("1. Add course");
		System.out.println("2. Add student");
		System.out.println("3. Enroll student into course");
		System.out.println("4. List students by age");
		System.out.println("5. List students by course");
		System.out.print("Enter your selection: ");
		return input.nextInt();
	}
	
	private void addCourse() {
		System.out.println("Adding a course...");
		System.out.print("Enter course name: ");
		String name = input.next();
		courses.add(new Course(name));
		System.out.println("Course " + name + " added.");
	}
	
	private void addStudent() {
		System.out.println("Adding a student...");
		Student stud = new Student();
		System.out.print("Student name: ");
		stud.name = input.next();
		System.out.print("Student id: ");
		stud.id = input.nextInt();
		System.out.print("Student age: ");
		stud.age = input.nextInt();
		System.out.print("Student phone: ");
		stud.phone = input.next();
		students.add(stud);
		System.out.println("Student " + stud.name + " added.");
	}
	
	private void enroll() {
		System.out.println("Enrolling student into course...");
		
		System.out.println("Select student:");
		for (int i=0; i < students.size(); ++i) {
			Student s = /*(Student)*/students.get(i);
			System.out.println((i+1) + ". " + s.toString());
		}
		int studIndex = input.nextInt() - 1;
		
		System.out.println("Select course:");
		for (int i=0; i < courses.size(); ++i) {
			Course c = /*(Course)*/courses.get(i);
			System.out.println((i+1) + ". " + c.toString());
		}
		int courseIndex = input.nextInt() - 1;
		
		Course course = /*(Course)*/courses.get(courseIndex);
		Student stud = /*(Student)*/students.get(studIndex);
		course.addStudent(stud);
		System.out.println("Student " + stud.name + " enrolled in " + course.getName());
	}
	
	public void listByAge() {
		ArrayList<Student> list = new ArrayList<Student>(students);
		Collections.<Student>sort(list, new StudentAgeComparator());
		System.out.println("Students by age:");
		for (int i=0; i < list.size(); ++i) {
			Student s = /*(Student)*/list.get(i);
			System.out.println(s.toString());
		}
	}
	
	public void listByCourse() {
		System.out.println("Students by course:");
		for (int i=0; i < courses.size(); ++i) {
			Course c = /*(Course)*/courses.get(i);
			System.out.println(c.toString());
			ArrayList<Student> list = c.getStudents();
			for (int j=0; j < list.size(); ++j) {
				Student s = /*(Student)*/list.get(j);
				System.out.println("  " + s.toString());
			}
		}
	}
	
	public void operate() {
		int option = 0;
		while ((option = readOption()) > 0) {
			if (option == 1)
				addCourse();
			else if (option == 2)
				addStudent();
			else if (option == 3)
				enroll();
			else if (option == 4)
				listByAge();
			else if (option == 5)
				listByCourse();
			else
				System.out.println("Unknown option.");
		}
		System.out.println("College is closing. Good bye.");
	}
}

public class Main {
	public static void main(String[] args) {
		College college = new College();
		college.operate();
	}
}

Thank you so much for being so kind and helpful!

This is just perfect!

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.