Question

In: Computer Science

A Lecturer wishes to create a program that lists his students sorted by the number of...

A Lecturer wishes to create a program that lists his students sorted by the number of theory assignment marks they have completed. The listing should be greatest number of assignment first, sub-sorted by name in lexicographical order (A to Z). A class Student stores the name and marks of assignment completed for a student.                                                    [5 marks]

Note: Assume the variable name of collection is list.

      i.         Provide a definition of Student with an equals() method. The comparison is based on name of the student.                                                                                     [3 marks]

    ii.         Write java statement to declare a collection variable list of type Student             [2 marks]

   iii.         Implements interface Comparator such that fulfil a natural ordering that matches the given requirement. Define method compareTo() such that it compares based on greatest number of assignment first, sub-sorted by name in lexicographical order (A to Z)). [5 marks]

   iv.         Provide definition of find(String) method which returns the index if given name exists in collection.                                                                                                     [2 marks]

     v.         Provide a static method in application class to display all students by calling toString method. Assume the name of object of collection is list.                             [4 marks]

Solutions

Expert Solution

Application class:

import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;

public class Application {
   public static void main(String[] args) {
       List<Student> list = new ArrayList<Student>();
       list.add(new Student("sai",90));
       list.add(new Student("kumar",85));
       list.add(new Student("abhilash",74));
       list.add(new Student("venkat",68));
       list.add(new Student("jay",90));
       list.add(new Student("alex",60));
       list.add(new Student("jonas",90));
       list.add(new Student("magnus",57));
       Collections.sort(list);
       Iterator iterator = list.iterator();
       while(iterator.hasNext()) {
       System.out.println(iterator.next());
       }
   }

}

Student class:

import java.util.Comparator;

public class Student implements Comparable<Student> {
  
   private String name;
   private int marks;

   public Student(String name, int marks) {
       super();
       this.name = name;
       this.marks = marks;
   }

   @Override
   public boolean equals(Object obj) {
       if (this == obj)
           return true;
       if (obj == null)
           return false;
       if (getClass() != obj.getClass())
           return false;
       Student other = (Student) obj;
       if (marks != other.marks)
           return false;
       if (name == null) {
           if (other.name != null)
               return false;
       } else if (!name.equals(other.name))
           return false;
       return true;
   }

   @Override
   public int compareTo(Student s2) {
       if (this.marks < s2.marks)
           return 1;
           else if (this.marks > s2.marks)
           return -1;
           else
           {
           if((this.name).compareToIgnoreCase(s2.name)<0)
           return -1;
           else if((this.name).compareToIgnoreCase(s2.name)>0)
           return 1;
           else
           return 0;
           }
   }
  
  
  
  
   @Override
   public String toString() {
   return "Student [name=" + name + ", marks=" + marks + "]";
   }   
     

}

output:

1.public class Student {

   String name;
   int marks;
  
   @Override
   public boolean equals(Object obj) {
       if (this == obj)
           return true;
       if (obj == null)
           return false;
       if (getClass() != obj.getClass())
           return false;
       Student other = (Student) obj;
       if (name == null) {
           if (other.name != null)
               return false;
       } else if (!name.equals(other.name))
           return false;
       return true;
   }
  

}

2.List<Student> list = new ArrayList<Student>();

3.class StudentComparator implements Comparator<Student>{
  
  
public int compare(Student s1, Student s2) {
if (s1.marks < s2.marks)
return 1;
else if (s1.marks > s2.marks)
return -1;
               else
{
if((s1.name).compareToIgnoreCase(s2.name)<0))
                   return -1;
                   else if((s1.name).compareToIgnoreCase(s2.name)>0))
                   return 1;
}
               return 0;  
}
}
      
      
4. int position = students.indexof(name)

5.ToString method:
@Override
   public String toString() {
       return "Student [name=" + name + ", marks=" + marks + "]";
   }
  
Printing all students:

       Collections.sort(list);
       Iterator iterator = list.iterator();
       while(iterator.hasNext()) {
       System.out.println(iterator.next());
       }
  

}


Related Solutions

Create a program that creates a sorted list from a data file. The program will prompt...
Create a program that creates a sorted list from a data file. The program will prompt the user for the name of the data file. Create a class object called group that contains a First Name, Last Name, and Age. Your main() function should declare an array of up to 20 group objects, and load each line from the input file into an object in the array. The group class should have the following private data elements: first name ,last...
c++ Create a program that creates a sorted list from a data file. The program will...
c++ Create a program that creates a sorted list from a data file. The program will prompt the user for the name of the data file. Create a class object called group that contains a First Name, Last Name, and Age. Your main() function should declare an array of up to 20 group objects, and load each line from the input file into an object in the array. The group class should have the following private data elements: first name...
Create a report that lists each topping and also lists the number of pizzas that used...
Create a report that lists each topping and also lists the number of pizzas that used that topping. Order the report in decreasing order of number of pizzas. That is, the most popular toppings will be at the top of the report.2 In each row of the table list: the topping name; the price of the topping; the number of pizzas that used the topping; and, the total value of the topping (number of pizzas times topping price). Since some...
c. Taking every 20th passenger in your sorted table, create a new table that lists Passenger...
c. Taking every 20th passenger in your sorted table, create a new table that lists Passenger ID and Rating for the 20 data points you will now have (In excel please) with formula. I am stomped by this. TABLE C5-1: PASSENGER ID and CUSTOMER EXPERIENCE RATING Passenger ID Rating (1-5) T006 4 T007 5 T008 1 T009 4 T010 5 T011 3 T012 2 T013 3 T014 2 T015 3 T016 4 T017 4 T018 4 T019 5 T020 3...
(JAVA) Create a program that takes in 15 numbers in sorted order from the console and...
(JAVA) Create a program that takes in 15 numbers in sorted order from the console and stores them in a 1D array of size 15. Next, prompt the user for a number to search for in the array (target). Then, print the array. Next, search the array using a linear search – printing out each of the indices (or “indexes”) that are being examined until the algorithm either finds the target or doesn’t. Then, do the same thing for a...
what are Sorted lists and their efficiency? in c++ and data structures
what are Sorted lists and their efficiency? in c++ and data structures
Topic: Students will be able to create skills in the use of linked lists, the stack,...
Topic: Students will be able to create skills in the use of linked lists, the stack, and the queue abstract data types, by implementing solutions to fundamental data structures and associated problems. Add the code for the methods where it says to implement. The main class is already done. There is a sample of the output. 1. A double-ended queue, or deque, is a data structure consisting of a list of items on which the following operations are defined: addToBack(x):...
Using C++, you will create a program, where you will create two doubly linked lists. These...
Using C++, you will create a program, where you will create two doubly linked lists. These doubly linked lists will contain integers within them. Using the numbers in both of these linked lists, you add the numbers together, and insert the addition of the two numbers into a singly linked list. the input can be from the user or you just write the input. for example, if one number in the doubly linked list is 817 and in the other...
How to do this in Python (using Lists): Create a python program that allows a user...
How to do this in Python (using Lists): Create a python program that allows a user to display, sort and update as needed a List of U.S States containing the State Capital and State Bird. You will need to embed the State data into your Python code. The user interface will allow the user to perform the following functions: 1. Display all U.S. States in Alphabetical order along with Capital and Bird 2. Search for a specific state and display...
The table lists the number of students from three different high schools participating in the mathematics...
The table lists the number of students from three different high schools participating in the mathematics and physics sections of a science fair High School 1 High School 2 High School 3 Mathematics 7 7 18 Physics 37 17 21 Given the following results. a) State the alternative hypothesis statement. (1 mark) b) State the degrees of freedom. (1 mark) c) Find the value of A, B, and C. d) Using the p-value method, at α = 0.05, test the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT