Question

You should create a driver class that calls the implementation in a separate class. You should...

You should create a driver class that calls the implementation in a separate class. You should not rely on static methods in the implementation class.

Specific methods to be defined/implemented

1. Create and use each primitive type.

2. Create a constant that can be used as a sentinel.

3. Use an if to control program execution.

4. Use a for, while and do while loop in your program.

5. Create an array in your program, add values to it and print it.

Specific Requirements: Your program should allow the user to enter a selection for the different steps in your program. For example, one of your conditions could be to allow the user to enter a list of names. Another could be to print this list of names. You could add a sort routine, to sort in ascending order. There are no requirements to execute specific functionality, you must use each of the concepts listed above.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Solution: There are total 2 files (Driver.java and Student.java)

Driver.java

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

//Driver class to run the program
public class Driver {
    public static void main(String[] args) throws IOException {

        Student obj = new Student();

        System.out.println("Please Enter your choice as below \n");
        System.out.println("Enter (1) to enter list of names separated by space");
        System.out.println("Enter (2) to print list of names using for loop");
        System.out.println("Enter (3) to print list of names using while loop");
        System.out.println("Enter (4) to print all names in ascending order");
        System.out.println("Enter (5) to print all names in descending order");
        System.out.println("Enter (0) to exit");
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
        System.out.println("\n\nEnter your choice\n");
        int choice = Integer.parseInt(bufferedReader.readLine());
        do
        {
            switch (choice)
            {
              case 0:
                  System.exit(1);
                    break;
              case 1:
                  System.out.println("\nEnter Names\n");
                  String[] namelist = bufferedReader.readLine().split(" ");
                  obj.addStudentNames(namelist);
                  break;
              case 2:
                  obj.printNamesUsingForLoop();
                  break;
              case 3:
                  obj.printNamesUsingWhileLoop();
                  break;
              case 4:
                  obj.printNamesAscendingOrder();
                  break;
              case 5:
                  obj.printNamesDescendingOrder();
                  break;
              default:
                  System.out.println("You have entered invalid choice ");
            }
            System.out.println("\n\nEnter your choice");
            choice = Integer.parseInt(bufferedReader.readLine());
        }while (choice >= 0 && choice  <= 5 );

    }
}

Student.java

import java.util.Arrays;
import java.util.Collections;

public class Student {
    private String studentnames[];
    private static int count = 0;
    //constant
    private final static String collegename = "UCLA";

    //Function to add the names to the array
    public void addStudentNames(String[] names){
        //Initialize only first time
        if(count == 0)
           studentnames = new String[100];
        for(String name : names)
        {
            studentnames[count++] = name;
        }
        System.out.println("Total Names Successfully Added = "+count);
    }

    //Function to print the name list using for loop
    public void printNamesUsingForLoop(){
        //check if name array is null
        if(studentnames == null) {
            System.out.println("\n\nSorry you don't have any names to print");
            return;
        }
        for(int i = 0; i < count; i++){
            System.out.println("Name : "+studentnames[i]+"   College : "+collegename);
        }
    }

    //Function to print the name list using while loop
    public void printNamesUsingWhileLoop(){
        //check if name array is null
        if(studentnames == null) {
            System.out.println("\n\nSorry you don't have any names to print");
            return;
        }
        int i = 0;
        while (i < count)
        {
            System.out.println("Name : "+studentnames[i]+"   College : "+collegename);
            i++;
        }
    }

    //Function will print the names in ascending order
    public void printNamesAscendingOrder(){
        //check if name array is null
        if(studentnames == null) {
            System.out.println("\n\nSorry you don't have any names to print");
            return;
        }
        String[] temparray = Arrays.copyOf(studentnames, count);
        //sort the elements in ascending order
        Arrays.sort(temparray);
        for(String name : temparray)
        {
            System.out.println("Name : "+name+"   College : "+collegename);
        }
    }

    //Function will print the names in descending order
    public void printNamesDescendingOrder(){
        //check if name array is null
        if(studentnames == null) {
            System.out.println("\n\nSorry you don't have any names to print");
            return;
        }
        String[] temparray = Arrays.copyOf(studentnames, count);
        //sort the elements in descending order
        Arrays.sort(temparray, Collections.reverseOrder());
        for(String name : temparray)
        {
            System.out.println("Name : "+name+"   College : "+collegename);
        }
    }
}

OUTPUT:

Please Enter your choice as below

Enter (1) to enter list of names separated by space
Enter (2) to print list of names using for loop
Enter (3) to print list of names using while loop
Enter (4) to print all names in ascending order
Enter (5) to print all names in descending order
Enter (0) to exit


Enter your choice

2


Sorry you don't have any names to print


Enter your choice
1

Enter Names

John Wilson Angelina Joli
Total Names Successfully Added = 4


Enter your choice
2
Name : John College : UCLA
Name : Wilson College : UCLA
Name : Angelina College : UCLA
Name : Joli College : UCLA


Enter your choice
3
Name : John College : UCLA
Name : Wilson College : UCLA
Name : Angelina College : UCLA
Name : Joli College : UCLA


Enter your choice
4
Name : Angelina College : UCLA
Name : John College : UCLA
Name : Joli College : UCLA
Name : Wilson College : UCLA


Enter your choice
5
Name : Wilson College : UCLA
Name : Joli College : UCLA
Name : John College : UCLA
Name : Angelina College : UCLA


Enter your choice
0

Process finished with exit code 1

Add a comment
Know the answer?
Add Answer to:
You should create a driver class that calls the implementation in a separate class. You should...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • IN C++ MUST INCLUDE HEADER FILE, IMPLEMENTATION FILE, AND DRIVER FILE. IN C++ Create a header...

    IN C++ MUST INCLUDE HEADER FILE, IMPLEMENTATION FILE, AND DRIVER FILE. IN C++ Create a header and implementation file to define an apartment class. Create a driver program to test the class, and create a make file to compile the driver program. Create two files called apartment.h and appartmentImp.cpp along with creating a driver program named testApartment.cpp containing the main function. Program Requirements: • Class attributes should include integers for number of rooms, monthly rent, and square feet, as well...

  • C++ Linked List Implementation Motivation As we discussed in class, the data structures that you use...

    C++ Linked List Implementation Motivation As we discussed in class, the data structures that you use to implement your program can have a profound impact on it's overall performance. A poorly written program will often need much more RAM and CPU time then a well-written implementation. One of the most basic data structure questions revolves around the difference between an array and a linked list. After you finish this assignment you should have a firm understanding of their operation. Problem...

  • Instructions We're going to create a program which will allow the user to add values to a list, p...

    Instructions We're going to create a program which will allow the user to add values to a list, print the values, and print the sum of all the values. Part of this program will be a simple user interface. 1. In the Program class, add a static list of doubles: private statie List<double> _values new List<double>) this is the list of doubles well be working with in the program. 2. Add ReadInteger (string prompt) , and ReadDouble(string prompt) to the...

  • C++ programming Phone Book Create a class that can be used for a Phone Book. The...

    C++ programming Phone Book Create a class that can be used for a Phone Book. The class should have attributes for the name and phone number. The constructor should accept a name and a phone number. You should have methods that allow the name to be retrieved, the phone number to be retrieved, and one to allow the phone number to be changed. Create a toString() method to allow the name and number to be printed. Write a program that...

  • In this assignment you will create two Java programs: The first program will be a class...

    In this assignment you will create two Java programs: The first program will be a class that holds information (variables) about an object of your choice and provides the relevant behavior (methods) The second program will be the main program which will instantiate several objects using the class above, and will test all the functions (methods) of the class above. You cannot use any of the home assignments as your submitted assignment. Your programs must meet the following qualitative and...

  • Create two classes. Song. Should include the name of the song and the artist Playlist. Should...

    Create two classes. Song. Should include the name of the song and the artist Playlist. Should include a list of song objects from the above class, a title for the list and a description of the list. Specific method requirements below. Functionality You should be able to print both a song and a playlist. Formatting should follow the below examples EXACTLY. I should be able to print a so11ng or a playlist by calling the standard python print function. Format...

  • Programmed in Java Pls - The other answer to this question is incorrect.: Your program should...

    Programmed in Java Pls - The other answer to this question is incorrect.: Your program should follow the below requirements: You will only need one class for this project called StudentReport in the package ilstu.edu In your class you will have the following instance variables: grades: a double 2d array that will hold the grades for all the students. Students: a 1d String array that will hold the names of all the students. In your class you will have the...

  • #PLEASE WRITE THE CODE IN JAVA! THANK YOU IN ADVANCE! Write a program that manages a...

    #PLEASE WRITE THE CODE IN JAVA! THANK YOU IN ADVANCE! Write a program that manages a list of up to 10 players and their high scores in the computer's memory. Use two arrays to manage the list. One array should store the players' names, and the other array should store the players' high scores. Use the index of the arrays to correlate the names with the scores. Your program should support the following features: a. Add a new player and...

  • Urgent must be done in C# create an instance of the “ testquestion” class in your main method. al...

    Urgent must be done in C# create an instance of the “ testquestion” class in your main method. allow the uswr to enter the teat question and its answer, it they want to. if they do not, then continue by creating a default queatuon and answer. please use the constructor methods here. Then, using the “tostring or _str_method, have it print out those variables as decribed above. call the “check answer” method and allow the user to continue attempting to...

  • ****Please read the following requirements and use java language w/ comments**** ****Can make this a multi...

    ****Please read the following requirements and use java language w/ comments**** ****Can make this a multi part question if needed**** Project requires a base class, a derived subclass, and an interactive driver (class with a main) that allows the user to utilize and manipulate the classes created. You may select one super class from the following:               Superclass                                    Example subclasses Ship class                                a) battleship, tugboat, icebreaker Person class                           b) manager, salaryworker, hourlyworker Aircraft class                         c) Learjet, cargoplane,...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT