Question

In: Computer Science

Q1) Create a program that do the following: 1. Asks the user to enter n marks...

Q1) Create a program that do the following:

1. Asks the user to enter n marks for n students, read the marks and the names and store them in a double linked list.

2. Write a method to find the largest mark and print the name of the student having that mark

3. Write a method to print the content of the list (name, mark)

4. Write a method to search the list for a given mark and prints the result

6. Insert 2 new students to the list (print the list before and after the insertion)

7. Delete any students with the first letter "D" in his name, (print the list before and after the deletion)

Submit .java files only.

Solutions

Expert Solution

Create program that do the following:

1. Asks the user to enter n marks for n students, read the marks and the names and store them in a double linked list.


Explanation:

LinkedList.java

class Node{

protected int marks;

protected String name;

protected Node next, prev;

public Node(){

next = null;

prev = null;

marks = 0;

name = null;

}

public Node(String name, int marks, Node next, Node prev){

this.next = next;

this.prev = prev;

this.name = name;

this.marks = marks;

}

public Node getNext(){

return this.next;

}

public void setNext(Node next){

this.next = next;

}

public Node getPrev(){

return this.prev;

}

public void setPrev(Node prev){

this.prev = prev;

}

public void setMarks(int marks){

this.marks = marks;

}

public int getMarks(){

return this.marks;

}

public void setName(String name){

this.name = name;

}

public String getName(){

return this.name;

}

}

public class LinkedList{

protected Node start;

protected Node end;

public int size;

public LinkedList(){

this.start = null;

this.end = null;

this.size = 0;

}

public Node getStart(){

return this.start;

}

public Node getEnd(){

return this.end;

}

public void insertAtStart(String name, int marks){

Node ptr = new Node(name, marks, null, null);

if(start == null){

start = ptr;

end = start;

}

else

{

start.setPrev(ptr);

ptr.setNext(start);

start = ptr;

}

size++;

}

public void insertAtEnd(String name, int marks){

Node ptr = new Node(name, marks, null, null);

if(start == null){

start = ptr;

end = start;

}

else{

end.setNext(ptr);

ptr.setPrev(end);

end = ptr;

}

size++;

}

public void deleteWithName(String name){

Node ptr = start;

while(!ptr.getName().equals(name))

ptr = ptr.getNext();

if(ptr.equals(start)){

start = ptr.getNext();

}

else if(ptr.equals(end)){

end = ptr.getPrev();

end.setNext(null);

}

else{

ptr.getPrev().setNext(ptr.getNext());

}

}

public void printHigestMark(){

int highest = 0;

String name = null;

Node ptr = start;

for( int i = 0; i< size; i++){

if(highest < ptr.getMarks()){

highest = ptr.getMarks();

name = ptr.getName();

ptr = ptr.getNext();

}

}

System.out.println(name + " has highest marks : " + highest);

}

public void search(int marks){

Node ptr = start;

for( int i=0; i<size; i++){

if( marks == ptr.getMarks())

System.out.println("Student : " + ptr.getName() + " has marks : " + ptr.getMarks());

ptr = ptr.getNext();

}

}

public void deleteWithFirstLetterD(){

Node ptr = start;

String name;

for( int i=0; i<size; i++){

name = ptr.getName();

if(name.startsWith("D") || name.startsWith("d")){

this.deleteWithName(name);

}

ptr = ptr.getNext();

}

}

public void show(){

Node ptr = start;

if(start == null )

System.out.println("LINKED LIST IS EMPTY ");

else{

if(start.equals(end)){

System.out.println("Name: " + ptr.getName() + "\tMarks : " + ptr.getMarks());

}

else{

while(!ptr.equals(end)){

System.out.println("Name: " + ptr.getName() + "\tMarks : " + ptr.getMarks());

ptr = ptr.getNext();

}

System.out.println("Name: " + ptr.getName() + "\tMarks : " + ptr.getMarks());

}

}

}

}

Main.java

import java.util.Scanner;

class Main {

public static void main(String[] args) {

LinkedList studentList = new LinkedList();

Scanner input = new Scanner(System.in);

System.out.println("Enter the number of students : ");

int n = Integer.parseInt(input.nextLine());

System.out.println("Enter the name and marks of the students ");

int tempMarks = 0;

String tempName = null;

for(int i = 1;i<=n;i++){

System.out.println("Name : ");

tempName = input.nextLine();

System.out.println("Marks : ");

tempMarks = Integer.parseInt(input.nextLine());

studentList.insertAtEnd(tempName, tempMarks);

}

studentList.printHigestMark();

studentList.show();

studentList.search(84);

System.out.println("List before insertion ");

studentList.show();

studentList.insertAtEnd("Hagrid", 10);

studentList.insertAtEnd("Peter", 40);

System.out.println("List after insertion ");

studentList.show();

System.out.println("DELETING ");

System.out.println("List before deletion");

studentList.show();

System.out.println("List after deletion ");

studentList.deleteWithFirstLetterD();

studentList.show();

}

}

Output:


Related Solutions

Q1 Create a program that asks the user to input their budget. Then, it will ask...
Q1 Create a program that asks the user to input their budget. Then, it will ask the user to input each of their purchase until their entire budget is used up. What kind of loop do you think is the most appropriate? You don't have to create the entire program. Part of it has already been created for you (see code below). Note: Assume that the user always uses up their budget to the last cent and they don't over...
Create a Java program that asks a user to enter two file names. The program will...
Create a Java program that asks a user to enter two file names. The program will read in two files and do a matrix multiplication. Check to make sure the files exist. first input is the name of the first file and it has 2 (length) 4 5 6 7 Second input is the name of the second file and it has 2 (length) 6 7 8 9 try catch method
a). Write a program that asks the user to enter an integer N and prints two...
a). Write a program that asks the user to enter an integer N and prints two integers, root and power, such that 1 < power < 6 and N = root ** power. If no such pair of integers exists, it should print a message to that effect. There are two loops, one for power and one for root. Order the loops so that if N = 64, then your program find that N = 8 ** 2 rather than...
Write a program that does the following in order: 1. Asks the user to enter a...
Write a program that does the following in order: 1. Asks the user to enter a name 2. Asks the user to enter a number “gross income” 3. Asks the user to enter a number “state tax rate” 4. Calculates the “Federal Tax”, “FICA tax” and “State tax” 5. Calculates the “estimated tax” and round the value to 2 decimal places 6. Prints values for “name”, “gross income” and “estimated tax” The program should contain three additional variables to store...
Write a C program that does the following: 1) Asks the user to enter an integer...
Write a C program that does the following: 1) Asks the user to enter an integer N 2) Asks the user to enter three words with at least N letters and at most 20 letters 3) Prints the first N letters from the first word 4) Prints the last N letters from the second word 5) Prints all letters in the odd position in the third word Output: Please enter an integer N: 3 Please enter a word with at...
Question Write a C program that asks the user to enter two integers x and n....
Question Write a C program that asks the user to enter two integers x and n. Then the program computes xn (=x * x * x …… (n times)) using for loop. and give me an output please use printf and scanf #include int main(void) {     //Declare required variables             //read two integers x , n from the keyboard                 //compute xn using for loop                     printf("< Your name >\n");...
plz use doubly linked list. java Q1) Create a program that do the following: 1. Asks...
plz use doubly linked list. java Q1) Create a program that do the following: 1. Asks the user to enter n marks for n students, read the marks and the names and store them in a double linked list. 2. Write a method to find the largest mark and print the name of the student having that mark 3. Write a method to print the content of the list (name, mark) 4. Write a method to search the list for...
Using Python Write a program that does the following in order: 1.     Asks the user to enter...
Using Python Write a program that does the following in order: 1.     Asks the user to enter a name 2.     Asks the user to enter a number “gross income” 3.     Asks the user to enter a number “state tax rate” 4.     Calculates the “Federal Tax”, “FICA tax” and “State tax” 5.     Calculates the “estimated tax” and round the value to 2 decimal places 6.     Prints values for “name”, “gross income” and “estimated tax” The program should contain three additional variables to store the Federal tax, FICA...
Write a C++ program that asks the user to enter the monthly costs for the following...
Write a C++ program that asks the user to enter the monthly costs for the following expenses incurred from operating your automobile: loan payment, insurance, gas, oil, tires, and maintenance. The program should then display the total monthly cost of these expenses, and a projected total annual cost of these expenses. Label each cost. The labels should be left aligned and have a column width of 30 characters. The cost should be aligned right and displayed with two decimal places...
Write a program that asks the user to enter the monthly costs for the following expenses...
Write a program that asks the user to enter the monthly costs for the following expenses incurred from operating his or her automobile: Loan payment Insurance Gas Oil Tires Maintenance Write a function that takes these six items as arguments and computes the total cost of these expenses. Write a main program that asks the user for these six items and calls your function. Print total monthly cost and the total annual cost for these expenses.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT