3

I'm really confused about Java classes and driver programs. What is the syntax of the code for a driver program and what should and should not be inside it? For the driver program we need perhaps to have a main program inside it but what about the Java class, don't we need to have any?

Do I need to have this for both the Java class and the driver program or only in the driver:

public static void main(String[] args) {

}
user16217248
  • 3,119
  • 19
  • 19
  • 37
Mah1990
  • 49
  • 1
  • 1
  • 4

2 Answers2

7

As referenced from here:

What is a "driver class"?

A "Driver class" is often just the class that contains a main. In a real project, you may often have numerous "Driver classes" for testing and whatnot, or you can build a main into any of your objects and select the runnable class through your IDE, or by simply specifying "java classname."

Example:

This is not a driver class since it doesn't contain any main method. In this case, it has the method hello:

public class HelloWorld {
    public void hello() {
        System.out.println("Hello, world!");
    }
}

Versus this one - which is a driver class since it contains a main method, and is the class that runs HelloWorld:

public class HelloWorldDriver {
    public static void main(String[] args) {
        HelloWorld sayhello = new HelloWorld();
        sayhello.hello();
    }
}

Hence the name "driver class" - as the class HelloWorldDriver "drives" or rather, controls the instantiation and usage of the class HelloWorld.

user16217248
  • 3,119
  • 19
  • 19
  • 37
jrd1
  • 10,358
  • 4
  • 34
  • 51
  • which mean that i have to write all the methods and constructors in the class and that i might invoke my methods and the main method from the driver class .? – Mah1990 Mar 10 '13 at 04:20
  • 1
    @Mah1990, Correct. You will have to write all your methods in one class, and then instantiate that class and use its methods and constructors from the main method of the driver class. – jrd1 Mar 10 '13 at 04:31
  • jrd1 , Thank you...i'm gonna try my code in see what's gonna happen. – Mah1990 Mar 11 '13 at 02:49
  • I have looked at your link. It propose the specification of a JDBC driver. Something which handle the JDBC interface. https://docs.oracle.com/javase/6/docs/api/java/sql/Driver.html no relationship with `main`. – Frédéric LOYER Apr 11 '23 at 20:40
  • @FrédéricLOYER Thank you for your comment. The URL you've included is one from the related answers to the one I referenced. Please note that the answer to which you're referring clearly states that *without context* a "driver class" *could refer* the SQL driver class. In this case, it's not based on usage at the time (also evidenced by the content of the related answer to the linked question). Granted, things have changed since this answer was posted in 2013. Now, the nomenclature and usage has shifted to refer to the JDBC class as evidenced by Google – jrd1 Apr 11 '23 at 21:08
  • I havn't understood "Now, the nomenclature and usage has shifted to refer to the JDBC class as evidenced by Google". Do you mean "driver" no longer designate uniquely dynamically loaded classes which implements an interface (JDBC is only one example). This is a common meaning (a OS driver is similar : a bunch of code which implement an interface) – Frédéric LOYER Apr 11 '23 at 21:13
  • @FrédéricLOYER It still is that - what I mean is that over time the Google results are more likely to show the JDBC driver and related classes. Namely, the results returned by a Google search without context are now more likely to show the JDBC results higher up – jrd1 Apr 11 '23 at 21:17
-2
import java.util.ArrayList;
import java.util.Scanner;
public class Driver {
    private static ArrayList<Dog> dogList = new ArrayList<Dog>();
    // Instance variables (if needed)
    private static ArrayList<Monkey> monkeyList = new ArrayList<Monkey>();
    public static void main(String[] args) {
        String userInput;

        initializeDogList();
        initializeMonkeyList();
        displayMenu();
        Scanner scanner = new Scanner(System.in);
        userInput = scanner.next().toUpperCase();
        while (userInput != "Q") {
            if ((userInput != "1") && (userInput != "2") && (userInput != "3") 
                    && (userInput != "4") && (userInput != "5") && (userInput != "6")) {
                System.out.println("Invalid Entry");
                displayMenu();
                userInput = scanner.next().toUpperCase();
            }
            else if (userInput == "1") {
                intakeNewDog(scanner);
                break;
            }
            else if (userInput == "2") {
                intakeNewMonkey(scanner);
                break;
            }
            else if (userInput == "3") {
                reserveAnimal(scanner);
                break;
            }
            else if (userInput == "4") {
                int number = Integer.parseInt(userInput);
                printAnimals(number);
                break;
            }
            else if (userInput == "5") {
                int number = Integer.parseInt(userInput);
                printAnimals(number);
                break;
            }
            else if (userInput == "6") {
                int number = Integer.parseInt(userInput);
                printAnimals(number);
                break;
            }
        }
    }
    // This method prints the menu options
    public static void displayMenu() {
        System.out.println("\n\n");
        System.out.println("\t\t\t\tRescue Animal System Menu");
        System.out.println("[1] Intake a new dog");
        System.out.println("[2] Intake a new monkey");
        System.out.println("[3] Reserve an animal");
        System.out.println("[4] Print a list of all dogs");
        System.out.println("[5] Print a list of all monkeys");
        System.out.println("[6] Print a list of all animals that are not reserved");
        System.out.println("[q] Quit application");
        System.out.println();
        System.out.println("Enter a menu selection");
    }
    // Adds dogs to a list for testing
    public static void initializeDogList() {
        Dog dog1 = new Dog("Moca", "German Shepherd", "male", "1", "25.6", "05-12-2019", "United States", "intake", false, "United States");
        Dog dog2 = new Dog("Smokey", "Great Dane", "male", "3", "35.2", "02-03-2020", "United States", "Phase I", false, "United States");
        Dog dog3 = new Dog("Pickles", "Chihuahua", "female", "4", "25.6", "12-12-2019", "Canada", "in service", true, "Canada");

        dogList.add(dog1);
        dogList.add(dog2);
        dogList.add(dog3);
    }

    // Adds monkeys to a list for testing
    //Optional for testing
    public static void initializeMonkeyList() {
        Monkey monkey1 = new Monkey("Chuckles", "Mandorin", "Female", "2", "200", "8'3", "2'", "Normal", "4/10/2023", "South America", "trained", "reserved", "service");
        Monkey monkey2 = new Monkey("Nitro", "Chimpanzee", "Male", "3", "400", "7", "10'", "Muscular", "4/10/2023", "United States", "Rescue", "reserved", "service Bouncer");
        Monkey monkey3 = new Monkey("Flower", "Mandorin", "Female", "2", "50", "2", "6'","Normal", "4/10/2023", "Turkey", "trained", "Rescue", "service");
        
        monkeyList.add(monkey1);
        monkeyList.add(monkey2);
        monkeyList.add(monkey3);

    }
    public static void intakeNewDog(Scanner scanner) {
        System.out.println("What is the dog's name?");
        String name = scanner.nextLine();
        for(Dog dog: dogList) {
            if(dog.getName().equalsIgnoreCase(name)) {
                System.out.println("\n\nThis dog is already in our system\n\n");
                return; //returns to menu
            }
        }
        System.out.println("What is the dog's breed?");
        String breed = scanner.nextLine();
        System.out.println("What is the dog's gender?");
        String gender = scanner.nextLine();
        System.out.println("What is the dog's age?");
        String age = scanner.nextLine();
        System.out.println("What is the dog's weight?");
        String weight = scanner.nextLine();
        System.out.println("What is the dog's acquisition date?");
        String acquisitionDate = scanner.nextLine();
        System.out.println("What is the dog's acquisition location?");
        String acquisitionCountry = scanner.nextLine();
        System.out.println("What is the dog's training status?");
        String trainingStatus = scanner.nextLine();
        System.out.println("Is the dog reserved?");
        boolean reserved = scanner.nextBoolean();
        System.out.println("What is the dog's service country?");
        String inServiceCountry = scanner.nextLine();
        
        Dog newDog = new Dog(name, breed, gender, age, weight, acquisitionDate, acquisitionCountry, trainingStatus, reserved, inServiceCountry);
        dogList.add(newDog);
        System.out.println("Dog added to list");

    }
    public static void intakeNewMonkey(Scanner scanner) {
        System.out.println("What is the monkey's name?");
        String name = scanner.nextLine();
        for(Monkey monkey: monkeyList) {
            if(monkey.getName().equalsIgnoreCase(name)) {
                System.out.println("\n\nThis monkey is already in our system\n\n");
                return; //returns to menu
            }
        }
        System.out.println("What is the monkey's species?");
        String species = scanner.nextLine();
        System.out.println("What is the monkey's gender?");
        String gender = scanner.nextLine();
        System.out.println("What is the monkey's age?");
        String age = scanner.nextLine();
        System.out.println("What is the monkey's weight?");
        String weight = scanner.nextLine();
        System.out.println("What is the monkey's height?");
        String height = scanner.nextLine();
        System.out.println("What is the monkey's tail length?");
        String tail = scanner.nextLine();
        System.out.println("What is the monkey's body length?");
        String body = scanner.nextLine();
        System.out.println("What is the monkey's acquisition date?");
        String date = scanner.nextLine();
        System.out.println("What is the monkey's acquisition location?");
        String country = scanner.nextLine();
        System.out.println("What is the monkey's training status?");
        String training = scanner.nextLine();
        System.out.println("Is the monkey reserved?");
        boolean reserved = scanner.nextBoolean();
        System.out.println("What is the monkey's service country?");
        String service = scanner.nextLine();
        
        Monkey newMonkey = new Monkey(name, species, gender, age, weight, height, tail, body, date, country, training, reserved, service);
        monkeyList.add(newMonkey);
        System.out.println("Monkey added to list");
    }
    public static void reserveAnimal(Scanner scanner) {
        scanner.nextLine();
        System.out.println("Enter animal type: ");
        String animalType = scanner.nextLine();
        if (animalType.equalsIgnoreCase("Monkey")) {
            System.out.println("Enter the monkey's country of acquisiton: ");
            String country = scanner.nextLine();
            for(Monkey obj: monkeyList) {
                if(obj.getAcquisitionLocation().equalsIgnoreCase(country)) {
                    obj.setReserved(true);
                    System.out.println("This monkey is now reserved.");
                    return;
                }
            }
            System.out.println("The monkey entered is not in the list");
        }
        else if (animalType.equalsIgnoreCase("Dog")) {
            System.out.println("Enter the dog's country of acquisition: ");
            String country = scanner.nextLine();
            for(Dog obj: dogList){
                if(obj.getAcquisitionLocation().equalsIgnoreCase(country)){
                    obj.setReserved(true);
                    System.out.println("This dog is now reserved.");
                    return;
                }
            }
            System.out.println("The dog entered is not in the list");
        } else {
            System.out.println("Type not found");
        }
    }
    public static void printAnimals(int option) {
        if (option == '4') {
            System.out.println(dogList);
        }
        else if (option == '5') {
            System.out.println(monkeyList);
        }
        else if (option == '6') {
            // Iterates through dogs
            for (int i = 0; i < dogList.size(); i++) {
                if (dogList.get(i).getTrainingStatus().equals("in service") && 
        dogList.get(i).getReserved()==false) {
                    // Prints dogs that are in service and available
                    System.out.println(dogList.get(i));
                }
            }
            // Iterates through monkeys
            for (int i = 0; i < monkeyList.size(); i++) {
                if (monkeyList.get(i).getTrainingStatus().equalsIgnoreCase("in service") && 
            monkeyList.get(i).getReserved()==false) {
                    // Prints monkeys that are in service and available
                    System.out.println(monkeyList.get(i));
                }
            }
        }
    }
}
user16217248
  • 3,119
  • 19
  • 19
  • 37
rmazur7831
  • 11
  • 2