Create the following classes: 1. class Student that includes the following insta
ID: 644649 • Letter: C
Question
Create the following classes:
1. class Student that includes the following instance variables:
private int ID;
private double GPA;
Student left;
Student right;
Your class should have the following:
? A constructor that initializes the instance variables.
? set and get methods for the instance variables ID , GPA,left and right .
? setGPA() will produce an error message if GPA is not betwee
and 4.
2. class StudentTree that represents a binary tree includes the following instance
variable:
Student root;
Where root is the root of the binary tree containing students. The nodes are of type
Student. The key for ordering the tree is the Student
Explanation / Answer
public class StudentTree
{
public Student root;
public StudentTree()
{
root = null;
}
boolean insert(int ID, double GPA)
{
boolean done = false;
root = insert(root, ID, GPA, done);
if(done)
return true;
else
return false;
}
private Student insert(Student root2, int iD, double gPA, boolean done)
{
if(root2 == null)
{
done = true;
root2 = new Student(iD, gPA);
}
else if(iD < root2.getID())
{
root2.left = insert(root2.left, iD, gPA, done);
}
else if(iD > root2.getID())
{
root2.right = insert(root2.right, iD, gPA, done);
}
else
{}
return root2;
}
public Student find(int ID)
{
if(root == null)
return null;
Student s = find(ID, root);
if(s == null)
return null;
else
return new Student(s.getID(), s.getGPA());
}
private Student find(int ID, Student root2)
{
Student current = root2;
if(current == null)
return null;
else if(ID == current.getID())
return current;
else if(ID < current.getID())
current = find(ID, current.left);
else
current = find(ID, current.right);
return current;
}
public boolean remove(int ID)
{
return false;
}
public void printTree()
{
printTree(root);
}
private void printTree(Student root2)
{
if(root != null)
{
if(root2.left != null)
printTree(root2.left);
if(root2.getID() == root.getID())
System.out.print("root(" + root2.getID() + ", " + root2.getGPA() + ") ");
else
System.out.print("(" + root2.getID() + ", " + root2.getGPA() + ") ");
if(root2.right != null)
printTree(root2.right);
}
System.out.println();
}
}
------------------------------------------------------------------------------------------------------------
import java.util.Scanner;
public class TreeTest
{
public static void main(String[] args)
{
StudentTree tree = new StudentTree();
Scanner input = new Scanner(System.in);
int choice;
int id;
double gpa;
do
{
System.out.println("Choose from the menu:");
System.out.println("1. Insert new student");
System.out.println("2. Find a student");
System.out.println("3. Remove a student");
System.out.println("4. Print Tree inorder");
System.out.println("5. Quit");
System.out.print("Enter your choice: ");
choice = input.nextInt();
switch(choice)
{
case 1:
System.out.println("Enter Students IDs and GPAs (-1 to stop):");
id = input.nextInt();
while(id != -1)
{
gpa = input.nextDouble();
tree.insert(id, gpa);
id = input.nextInt();
}
break;
case 2:
System.out.println("Enter Students IDs to be found (-1 to stop)");
System.out.print("ID: ");
id = input.nextInt();
while(id != -1)
{
Student s = tree.find(id);
if(s != null)
System.out.println("GPA: ");
else
System.out.println("Student not found!!");
System.out.print("ID: ");
}
break;
case 3:
System.out.println("Enter Student IDs to be removed (-1 to stop):");
System.out.print("ID: ");
id = input.nextInt();
while(id != -1)
{
boolean result = tree.remove(id);
if(result)
System.out.println(id + " is removed");
else
System.out.println("ERROR: The ID does not exist!!");
System.out.print("ID: ");
}
break;
case 4:
tree.printTree();
break;
case 5:
break;
default:
System.out.println("Invalid choice");
}
System.out.println();
}while(choice != 5);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.