0

My Code

package com.company;

import java.util.Scanner;
public class Main {

    public static void main(String[] args) {
        Tutor tutorOne = new Tutor("Anthony Joshua", "Mathematics", "31", "London, England"); //creating tutor objects until "till here" comment
        Tutor tutorTwo = new Tutor("Andy Ruiz", "Physics", "31", "Mexico City, Mexico");
        Tutor tutorThree = new Tutor("Vitali Klitschko", "Computer Science", "49", "Saint Petersberg, Russia");
        Tutor tutorFour = new Tutor("Ray Leonard", "Mathematics", "64", "North Carolina, USA"); // till here
        Tutor[] allTutors = {tutorOne, tutorTwo, tutorThree, tutorFour}; // defining an Array containing all the tutors.
        Scanner userInput = new Scanner(System.in); // user input
        System.out.println("Enter the subject: "); // the message user sees when program is run
        String tutorNeeded = userInput.toLowerCase().nextLine(); // reads input including the space between words
        for (int i = 0; i < allTutors.length; i++) { // for loop that iterates over allTutors
            Tutor t = allTutors[i];
            if (t.subject.contains(tutorNeeded)) {
                System.out.println(t.name + "\n" + t.subject + "\n" + t.age + "\n" + t.location); // prints all tutor attributes
            }
        }

        if (tutorNeeded.contains("math")) { // tutorNeeded.contains checks if these letters in this order is present in the user input
            System.out.println(tutorOne.name+"\n"+tutorOne.subject+"\n"+tutorOne.age+"\n"+tutorOne.location); // prints attributes for tutor one.
        }else if (tutorNeeded.contains("physics")) {
            System.out.println(tutorTwo.name+"\n"+tutorTwo.subject+"\n"+tutorTwo.age+"\n"+tutorTwo.location);
        } else if (tutorNeeded.contains(("computer"))) {
            System.out.println(tutorThree.name + "\n" + tutorThree.subject + "\n" + tutorThree.age + "\n" + tutorThree.location);
        } else if (tutorNeeded.contains("accounts")){
            System.out.println(tutorFour.name + "\n" + tutorFour.subject + "\n" + tutorFour.age + "\n" + tutorFour.location);
        } else {
            System.out.println("Sorry, we don't have any tutors for that subject available at this time.");
        }
    }
    static class Tutor { // defining second class
        String name; // defining properties of object tutors
        String subject;
        String age;
        String location;

        Tutor(String name, String subject, String age, String location) { // Tutor constructor to define objects used above
            // "this" refers to current instance of the current class
            this.name = name;
            this.subject = subject;
            this.age = age;
            this.location = location;
        }
    }
}

My University Professor says that this code has only one class. I understand that all the code is in one Main class, but wouldn't the Tutor Class also be counted as another class? Making it 2 classes in this code?

2 Answers2

3

Two classes, each compiled into their own file

Compile your code, and see for yourself. (After fixing that toLowerCase call.) Look at the files produced by the compiler.

The compiled code is in two files:

  • Main.class
  • Main$Tutor.class

Your Tutor class is nested within your Main class. That gets compiled using a joint name, combining outer class name and nested class name, separated by $. In your case, Main$Tutor.

➥ So, two classes here.

By the way, this nested-naming is the reason to avoid using $ character in your Java naming. While allowed in some circumstances, its use can be confusing. Ex: this and this.

My University Professor says that this code has only one class.

You likely misunderstood some other point your instructor was making.

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
1

Yes. You are correct. This class has two classes, though only one is a top-level class.

Louis Wasserman
  • 191,574
  • 25
  • 345
  • 413