School Project

Joined
Dec 8, 2022
Messages
1
Reaction score
0
I am working on a school project it keeps giving me an error in the main class do not know how to fix it :

package Pet;

import java.util.Scanner;
import java.util.HashMap;
import java.util.Map;

public class Pet {
private String petType;
private String petName;
private int petAge;
private Map<Pet, Integer> dogSpace; // contains the Pet and days it is staying
private Map<Pet, Integer> catSpace; // same but for cats

public double amountDue;


/**
* Pet, base class for Dog and Cat
* @param String name - Name of the Pet
* @param int age - Age of the Pet
* @param String type - Cat | Dog
*/
public Pet(String name, int age, String type) {
this.petName = name;
this.petAge = age;
this.petType = type;
this.dogSpace = new HashMap<Pet, Integer>(); // use a hashmap to keep track of pets in the shelter
this.catSpace = new HashMap<Pet, Integer>(); // the Pet object is the key, and the days they are staying is the value.
}

public void checkIn(Pet pet) {
Scanner in = new Scanner(System.in);
System.out.println("How many days will your " + this.petType + " be staying?");
int days = (int) in.nextInt();
switch(this.petType) {
case "Dog":
if(days > 1) {
System.out.println("Do you require grooming services?");
String needsGrooming = in.next();
boolean grooming;
if(needsGrooming.equals("yes") || needsGrooming.equals("y")) {
System.out.println("We will groom your dog...\n");
grooming = true;
}
else {
grooming = false;
}
this.checkInDog((Dog) pet, days, grooming); // handle the special dog cases
}
else {
this.checkInDog((Dog) pet, days, false);
}
break;
case "Cat":
if(this.catSpace.size() < 12) {
this.catSpace.put(pet, days);
}
break;
default: // Throw an exception if a pet other than a Cat | Dog is booked.
in.close(); // Close the scanner before exiting.
throw new RuntimeException("Sorry, we only accept cats and dogs");
}
in.close(); // Close the scanner when there is no exceptin thrown.
}

/**
* Contains extra steps for checking in a dog.
* @param pet - The dog object.
* @param days - Number of days staying.
* @param grooming - Whether or not the dog needs grooming.
*/
private void checkInDog(Dog pet, int days, boolean grooming) {
pet.setGrooming(grooming);

try {
if(this.dogSpace.size() < 30) { // Enforce the maximum of 30 dogs in the shelter.
this.dogSpace.put(pet, days);
pet.dogSpaceNbr = this.dogSpace.size() + 1;
pet.setDaysStay(days);
}
}
catch (Exception e) { // For some Map objects, calling size() on an empty collection can throw an error.
System.out.println("You're our first visitor!");
System.out.print(pet);
this.dogSpace.put(pet, days);
pet.dogSpaceNbr = 1;
}
System.out.println("" + pet.getPetName() + " will miss you, but is in good hands.");
}

/**
* Check out the desired Pet and calculate how much is owed for the boarding.
* @param pet - The pet you wish the check-out.
* @return amountDue - The amount of money owed for the boarding.
*/
public double checkOut(Pet pet) {
double fee;

if(pet.getPetType() == "Dog") {
double groomingfee = 0.0;
Dog animal = (Dog) pet;
int days = this.dogSpace.remove(pet);
double weight = animal.getDogWeight();
if(weight < 20) {
fee = 24.00;
if(animal.getGrooming()) {
groomingfee = 19.95;
}
} else if (weight > 20 && weight < 30) {
fee = 29.00;
if(animal.getGrooming()) {
groomingfee = 24.95;
}
} else {
fee = 34.00;
if(animal.getGrooming()) {
groomingfee = 29.95;
}
}
System.out.println("Fee Schedule:\n Boarding Fee: " + (fee*days) + "\nGrooming Fee: " + groomingfee);
animal.amountDue = (fee * days) + groomingfee;
return animal.amountDue;
}
else {
int days = this.catSpace.remove(pet);
fee = 18.00;
pet.amountDue = (fee * days);
return pet.amountDue;
}
}

public Pet getPet(Pet pet) { // Not sure why we need this.
return pet;
}

public Pet createPet(String name, int age, String type) {
switch(type) {
case "Dog":
return new Dog(name, age);
case "Cat":
return new Pet(name, age, "Cat"); // I have implemented the dog class, not the cat.
default:
throw new Error("Only Dogs and Cats can stay at this facility.");
}
}

/**
* Asks the user to fill in all of the attributes of a pet. Saves them directly to the object it was called on.
* @param pet - The pet you wish to update information on.
*/
public void updatePet(Pet pet) {
Scanner in = new Scanner(System.in);
System.out.println("What is the pets new name?");
pet.setPetName(in.next());
System.out.println("What is the pets age?");
pet.setPetAge(in.nextInt());
System.out.println("What type of animal is your pet?");
pet.setPetType(in.next());
in.close();
}

public String getPetName() {
return this.petName;
}

public int getPetAge() {
return this.petAge;
}

public String getPetType() {
return this.petType;
}

public void setPetName(String name) {
this.petName = name;
}

public void setPetAge(int age) {
this.petAge = age;
}

public void setPetType(String type) {
switch(type) { // while a switch is extra here, it will make it easier to add other pets.
case "Dog":
this.petType = type;
break;
case "Cat":
this.petType = type;
break;
}
}

public void setDaysStay(int days) {
}



public class Dog extends Pet {
public int dogSpaceNbr;
private double dogWeight;
private boolean grooming;

public Dog(String name, int age) { // automatically declares a pet of type Dog
super(name, age, "Dog"); // super is used to call the parent classes constructor
}

public double getDogWeight() {
return this.dogWeight;
}

public boolean getGrooming() {
return this.grooming;
}

public void setDogWeight(double weight) {
this.dogWeight = (double) weight; // casting a double here might be redundant, but it helps us to be sure
} // we don't get at type error

public void setGrooming(boolean value) {
this.grooming = value;
}
}



public class Cat extends Pet {
private int catSpaceNbr; // The number space the cat is in.

public Cat(String name, int age) { // automatically declares a pet of type Cat
super(name, age, "Cat"); // Calls the constructor of the parent class
}

public int getCatSpace() {
return this.catSpaceNbr;
}

public void setCatSpace(int number) {
this.catSpaceNbr = number;
}
}


public class Main {
public static void main(String args[]) {
Pet pet = new Pet("Spot", 12, "Dog" );
System.out.println("Pet name is " + pet.getPetName());

Dog dog = new Dog("Pepper", 15);
System.out.println("" + dog.getPetName() + " is " + dog.getPetAge() + " years old.");

Cat cat = new Cat("Bailey", 5);
System.out.println("" + cat.getPetName() + " is " + cat.getPetAge() + " years old.");

pet.checkIn(dog);

double price = pet.checkOut(dog);
System.out.println("You owe " + price + ".");
}
}

}

Error: Unable to load main class Pet.pet in module Pet
java.lang.NoClassDefFoundError: Pet/Pet (wrong name: Pet/pet)
 
Joined
Dec 16, 2022
Messages
5
Reaction score
3
It looks like the error is occurring in the checkIn method of the Pet class. The error message "Cannot make a static reference to the non-static method checkIn(Pet)" suggests that you are trying to call the checkIn method from a static context, such as from within a static method or from the main method of your program.

To fix this error, you will need to either make the checkIn method static, or create an instance of the Pet class and call the checkIn method on that instance. Here is an example of how you might do this:

Code:
Pet myPet = new Pet("Fluffy", 3, "Cat");
myPet.checkIn(myPet);

Alternatively, you could make the checkIn method static and call it like this:

Code:
Pet.checkIn(myPet);

Note that if you make the checkIn method static, you will also need to make the dogSpace and catSpace instance variables static, as they are currently being accessed from within the checkIn method.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,763
Messages
2,569,560
Members
45,035
Latest member
HoTaKeDai

Latest Threads

Top