狗类Java分配

e3bfsja2  于 2023-03-21  发布在  Java
关注(0)|答案(4)|浏览(107)

我是一名Java编程的学生,正忙碌一项作业。我真的很感激一些帮助。
下面是我必须创建的类:

/**
 * Class for basic dog data: name, age, weight, breed and booster shot info.
 */
public class Dog {
    private String name; //name of dog
    private int age; //in years
    private double weight;  //in pounds
    private String breed; //breed of dog
    private boolean boosterShot; //Status of booster shot

    public Dog() {
        name = "No name yet.";
        age = 0;
        weight = 0;
        breed = "No breed yet.";
        this.boosterShot = false;
    }

    public Dog(String initialName, int initialAge,
               double initialWeight, String initialBreed, boolean boosterShot) {
        name = initialName;
        if ((initialAge < 0) || (initialWeight < 0)) {
            System.out.println("Error: Negative age or weight.");
            System.exit(0);
        } else {
            age = initialAge;
            weight = initialWeight;
            breed = initialBreed;
            this.boosterShot = boosterShot;
        }
    }

    public Dog(String initialName, String initialBreed, boolean boosterShot) {
        name = initialName;
        age = 0;
        weight = 0;
        breed = initialBreed;
        this.boosterShot = boosterShot;
    }

    public Dog(int initialAge) {
        name = "No name yet.";
        weight = 0;
        breed = "No breed yet.";
        boosterShot = false;
        if (initialAge < 0) {
            System.out.println("Error: Negative age.");
            System.exit(0);
        } else
            age = initialAge;
    }

    public Dog(double initialWeight) {
        name = "No name yet.";
        age = 0;
        breed = "No breed yet.";
        boosterShot = false;
        if (initialWeight < 0) {
            System.out.println("Error: Negative weight.");
            System.exit(0);
        } else
            weight = initialWeight;
    }

    public void setDog(String newName, int newAge,
                       double newWeight, String newBreed, boolean boosterShot) {
        name = newName;
        if ((newAge < 0) || (newWeight < 0)) {
            System.out.println("Error: Negative age or weight.");
            System.exit(0);
        } else {
            age = newAge;
            weight = newWeight;
            breed = newBreed;
            this.boosterShot = boosterShot;
        }
    }

    public boolean hadBoosterShot() {
        return this.boosterShot;
    }

    public String getName() {
        return name;
    }

    public void setName(String newName) {
        name = newName; //age and weight are unchanged.
    }

    public int getAge() {
        return age;
    }

    public void setAge(int newAge) {
        if (newAge < 0) {
            System.out.println("Error: Negative age.");
            System.exit(0);
        } else
            age = newAge; //name and weight are unchanged.
    }

    public double getWeight() {
        return weight;
    }

    public void setWeight(double newWeight) {
        if (newWeight < 0) {
            System.out.println("Error: Negative weight.");
            System.exit(0);
        } else
            weight = newWeight; //name and age are unchanged.
    }

    public String getBreed() {
        return breed;
    }

    public void setBreed(String newBreed) {
        breed = newBreed;
    }

    public boolean getBoosterShot() {
        return boosterShot;
    }

    public void setBoosterShot(boolean boosterShot) {
        this.boosterShot = boosterShot;
    }

    public void writeOutput() {
        System.out.println("Name: " + name);
        System.out.println("Age: " + age + " years");
        System.out.println("Weight: " + weight + " pounds");
        System.out.println("Breed: " + breed);
        System.out.println("Booster shot status: " + boosterShot);
    }
}

下面是我创建的测试程序:

import java.util.Scanner;

public class DogDemo {
    public static void main(String[] args) {
        Dog a = new Dog();
        System.out.println("My records on your dog are inaccurate.");
        System.out.println("Here is what they currently say:");
        a.writeOutput();

        Scanner keyboard = new Scanner(System.in);

        System.out.println("Please enter the correct dog name:");
        String correctName = keyboard.nextLine();
        a.setName(correctName);

        System.out.println("Please enter correct dog breed:");
        String correctBreed = keyboard.nextLine();
        a.setBreed(correctBreed);

        System.out.println("Please enter the correct dog age:");
        int correctAge = keyboard.nextInt();
        a.setAge(correctAge);

        System.out.println("Please enter the correct dog weight:");
        double correctWeight = keyboard.nextDouble();
        a.setWeight(correctWeight);

        System.out.println("My updated records now say:");
        a.writeOutput();
    }
}

所有工作正常,但我真的卡住了如何设置boosterShottruefalse在测试程序。
任何帮助都将不胜感激。

vyu0f0g1

vyu0f0g11#

你可以用这个。试着问狗是不是Boostershot

System.out.println("Is this Boostershot? Yes or No");

String ans = keyboard.nextLine();

if (ans.equals("Yes")) {
    a.setBoosterShot(true);
} else if (ans.equals("No")) {
    a.setBoosterShot(false);
} else {
    System.out.print("Please Input Yes or No");
}
cclgggtu

cclgggtu2#

尝试将For循环。循环五次,然后将所有2岁以上的狗和booster= false放入狗列表中
导入java.util.linkedList;
导入java.util.List;
导入java.util.Scanner;
public class String {

static List<Dog> dogList=new LinkedList<>();

 public static void main(String[] args) {

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

    Dog a = new Dog();

    System.out.println("My records on your dog are inaccurate.");

    System.out.println("Here is what they currently say:");

    a.writeOutput();

    Scanner keyboard = new Scanner(System.in);

    System.out.println("Please enter the correct dog name:");

    String correctName = keyboard.nextLine();

    a.setName(correctName);

    System.out.println("Please enter correct dog breed:");

    String correctBreed = keyboard.nextLine();

    a.setBreed(correctBreed);

    System.out.println("Please enter the correct dog age:");

    int correctAge = keyboard.nextInt();

    a.setAge(correctAge);

    System.out.println("Please enter the correct dog weight:");

    double correctWeight = keyboard.nextDouble();

    a.setWeight(correctWeight);

    System.out.println("My updated records now say:");

    a.writeOutput();

    if(a.getAge()>2&&!a.getBoosterShot()){

    dogList.add(a);

    }

     }

     System.out.println("\n");

     System.out.println("List of all dogs that are over two years old and have not had their booster shots");

     for(Dog d:dogList)

     {

     System.out.println(".................\n");

      System.out.println("Dog Name: "+d.getName());

      System.out.println("Dog Breed: "+d.getBreed());

      System.out.println("Dog Age: "+d.getAge());

     }

 }

}

vh0rcniy

vh0rcniy3#

import java.util.Scanner;

public class Pet {
private int dogSpaces = 30;
private int catSpaces = 12;
private int petAge;

public Pet(int dogSpaces, int catSpaces) {
    this.dogSpaces = dogSpaces;
    this.catSpaces = catSpaces;
}

public int getDogSpaces() {
    return dogSpaces;
}

public int getCatSpaces() {
    return catSpaces;
}

public String checkInPet(String petType, int petWeight, int petAge, int lengthOfStay, boolean groomingRequested, boolean hasBeenHereBefore) {
    if (!hasBeenHereBefore) {
        return "Welcome! Please update your client information.";
    }

    if (petType.equals("dog")) {
        if (petWeight >= 30 && dogSpaces > 0) {
            dogSpaces--;
        } else if (petWeight >= 20 && dogSpaces > 0) {
            dogSpaces--;
        } else if (dogSpaces > 0) {
            dogSpaces--;
        } else {
            return "Sorry, there is no available space for your dog.";
        }
    } else if (petType.equals("cat")) {
        if (catSpaces > 0) {
            catSpaces--;
        } else {
            return "Sorry, there is no available space for your cat.";
        }
    } else {
        return "Invalid pet type.";
    }

    double groomingFee = 0.0;
    double boardingFee = 0.0;
    if (petType.equals("dog")) {
        if (lengthOfStay >= 2 && groomingRequested) {
            groomingFee = 29.95;
        }
        if (petWeight >= 30) {
            boardingFee = 34.00;
        } else if (petWeight >= 20) {
            boardingFee = 29.00;
        } else {
            boardingFee = 24.00;
        }
    } else if (petType.equals("cat")) {
        boardingFee = 18.00;
    }

    return String.format("Pet checked in successfully. Boarding fee: $%.2f/day. Grooming fee: $%.2f.", boardingFee, groomingFee);
}

public static void main(String[] args) {
    Pet pet = new Pet(30, 12);
    Scanner scanner = new Scanner(System.in);
    System.out.print("Is the pet a dog or a cat? ");
    String petType = scanner.nextLine();
    System.out.print("What is the weight of the pet? ");
    int petWeight = scanner.nextInt();
    scanner.nextLine();
    System.out.print("What is the age of the pet? ");
    int petAge = scanner.nextInt();
    scanner.nextLine();
    System.out.print("Has the pet been here before? (yes/no) ");
    String hasBeenHereBefore = scanner.nextLine();

    boolean hasBeenHereBeforeBool = false;
    if (hasBeenHereBefore.equalsIgnoreCase("yes")) {
        hasBeenHereBeforeBool = true;
    }

    String result = pet.checkInPet(petType, petWeight, petAge, 3, true, hasBeenHereBeforeBool);
    System.out.println(result);

    System.out.printf("Available dog spaces: %d\n", pet.getDogSpaces());
    System.out.printf("Available cat spaces: %d\n", pet.getCatSpaces());
}

}

hi3rlvi2

hi3rlvi24#

一种可能性:
a.setBoosterShot(true);

相关问题