Question

In: Computer Science

Java problem Implement class Course containing: String name, enumeration Type (Foundamental, Specialization, Discipline), enumeration Stream (English,...

Java problem

Implement class Course containing: String name, enumeration Type (Foundamental, Specialization, Discipline), enumeration Stream (English, French, German), int creditPoints.

 Class Contract has an array of courses with methods addCourse(Course), deleteCourse(type, stream, name) sort(), display().

 Courses are sorted by stream, type, name.

 If 2 courses are equal raise a custom exception in method sort().

 Make Contract implement Storable.

Solutions

Expert Solution

//Course.java

package test2;

public class Course {

   private String name;
   private enum Type {
       Foundamental, Specialization, Discipline;
   }
   private Type type;
  
   private enum Stream {
       English, French, German;
   }
   private Stream stream;
  
   private int creditPoints;

   public Course(){
       this.name = "Not set";
       this.type = Type.Discipline;
       this.stream = Stream.English;
       this.creditPoints = 0;
   }
  
   public Course(String name, String type, String stream, int creditPoints){
       this.name = name;
       this.type = Type.valueOf(type);
       this.stream = Stream.valueOf(stream);
       this.creditPoints = creditPoints;
   }

   public String getName() {
       return name;
   }

   public void setName(String name) {
       this.name = name;
   }

   public String getType() {
       return type.name();
   }

   public void setType(String type) {
       this.type = Type.valueOf(type);
   }

   public String getStream() {
       return stream.name();
   }

   public void setStream(String stream) {
       this.stream = Stream.valueOf(stream);
   }

   public int getCreditPoints() {
       return creditPoints;
   }

   public void setCreditPoints(int creditPoints) {
       this.creditPoints = creditPoints;
   }
  
   public String toString(){
       return "Name: " + this.name + "\n"
               + "Type: " + this.type.name() + "\n"
               + "Stream: " + this.stream.name() + "\n"
               + "Credit Points: " + this.creditPoints + "\n";
   }
}

//test.java
package test2;
public class test {
   public static void main(String[] args) {
       Course c1 = new Course("b","Discipline","English",5);
       Course c2 = new Course("a","Discipline","German",5);
       Course c3 = new Course("d","Foundamental","French",5);
       Course c4 = new Course("c","Discipline","English",5);
       Course c6 = new Course("f","Specialization","French",5);
       Contract obj = new Contract();
       obj.add(c1);
       obj.add(c2);
       obj.add(c3);
       obj.add(c4);
       obj.add(c6);
       obj.display();
       obj.sortByName();
       System.out.println("\nAfter sorting by Name\n");
       obj.display();
       obj.sortByType();
       System.out.println("\nAfter sorting by Type\n");
       obj.display();
       obj.sortByStream();
       System.out.println("\nAfter sorting by Stream\n");
       obj.display();
       Course c5 = new Course("c","Discipline","English",5);
       obj.add(c5);
       System.out.println("\nDuplicate value exception\n");
       obj.sortByName();
   }

}


Related Solutions

Java... Design a Payroll class with the following fields: • name: a String containing the employee's...
Java... Design a Payroll class with the following fields: • name: a String containing the employee's name • idNumber: an int representing the employee's ID number • rate: a double containing the employee's hourly pay rate • hours: an int representing the number of hours this employee has worked The class should also have the following methods: • Constructor: takes the employee's name and ID number as arguments • Accessors: allow access to all of the fields of the Payroll...
In Java, Here is a basic Name class. class Name { private String first; private String...
In Java, Here is a basic Name class. class Name { private String first; private String last; public Name(String first, String last) { this.first = first; this.last = last; } public boolean equals(Name other) { return this.first.equals(other.first) && this.last.equals(other.last); } } Assume we have a program (in another file) that uses this class. As part of the program, we need to write a method with the following header: public static boolean exists(Name[] names, int numNames, Name name) The goal of...
Create a class named BankAccount, containing: a constructor accepting a String corresponding to the name of...
Create a class named BankAccount, containing: a constructor accepting a String corresponding to the name of the account holder. a method, getBalance, that returns a double corresponding to the account balance. a method withdraw that accepts a double, and deducts the amount from the account balance. Write a class definition for a subclass, CheckingAccount, that contains: a boolean instance variable, overdraft. (Having overdraft for a checking account allows one to write checks larger than the current balance). a constructor that...
1. Circle: Implement a Java class with the name Circle. It should be in the package...
1. Circle: Implement a Java class with the name Circle. It should be in the package edu.gcccd.csis. The class has two private instance variables: radius (of the type double) and color (of the type String). The class also has a private static variable: numOfCircles (of the type long) which at all times will keep track of the number of Circle objects that were instantiated. Construction: A constructor that constructs a circle with the given color and sets the radius to...
Program in Java Create a class and name it MyArray and implement following method. * NOTE:...
Program in Java Create a class and name it MyArray and implement following method. * NOTE: if you need more methods, including insert(), display(), etc. you can also implement those. Method name: getKthMin(int k) This method receives an integer k and returns k-th minimum value stored in the array. * NOTE: Items in the array are not sorted. If you need to sort them, you can implement any desired sorting algorithm (Do not use Java's default sorting methods). Example: Items...
Java: Create a class and name it MyArray and implement following method. * NOTE: if you...
Java: Create a class and name it MyArray and implement following method. * NOTE: if you need more methods, including insert(), display(), etc. you can also implement those Method name: getKthMin(int k) This method receives an integer k and returns k-th minimum value stored in the array. * NOTE: Items in the array are not sorted. If you need to sort them, you can implement any desired sorting algorithm (Do not use Java's default sorting methods). Example: Items in the...
Program in Java Create a class and name it MyArray and implement following method. * NOTE:...
Program in Java Create a class and name it MyArray and implement following method. * NOTE: if you need more methods, including insert(), display(), etc. you can also implement those. Method name: getKthMin(int k) This method receives an integer k and returns k-th minimum value stored in the array. * NOTE: Items in the array are not sorted. If you need to sort them, you can implement any desired sorting algorithm (Do not use Java's default sorting methods). Example: Items...
JAVA The class will have a constructor BMI(String name, double height, double weight). The class should...
JAVA The class will have a constructor BMI(String name, double height, double weight). The class should have a public instance method, getBMI() that returns a double reflecting the person's BMI (Body Mass Index = weight (kg) / height2 (m2) ). The class should have a public toString() method that returns a String like Fred is 1.9m tall and is 87.0Kg and has a BMI of 24.099722991689752Kg/m^2 (just print the doubles without special formatting). Implement this class (if you wish you...
Here is a C++ class definition for an abstract data type LinkedList of string objects. Implement...
Here is a C++ class definition for an abstract data type LinkedList of string objects. Implement each member function in the class below. Some of the functions we may have already done in the lecture, that's fine, try to do those first without looking at your notes. You may add whatever private data members or private member functions you want to this class. #include #include typedef std::string ItemType; struct Node { ItemType value; Node *next; }; class LinkedList { private:...
Suppose we have a Java class called Person.java public class Person { private String name; private...
Suppose we have a Java class called Person.java public class Person { private String name; private int age; public Person(String name, int age) { this.name = name; this.age = age; } public String getName(){return name;} public int getAge(){return age;} } (a) In the following main method which lines contain reflection calls? import java.lang.reflect.Field; public class TestPerson { public static void main(String args[]) throws Exception { Person person = new Person("Peter", 20); Field field = person.getClass().getDeclaredField("name"); field.setAccessible(true); field.set(person, "Paul"); } }...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT