我是一名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();
}
}
所有工作正常,但我真的卡住了如何设置boosterShot
到true
或false
在测试程序。
任何帮助都将不胜感激。
4条答案
按热度按时间vyu0f0g11#
你可以用这个。试着问狗是不是Boostershot
cclgggtu2#
尝试将For循环。循环五次,然后将所有2岁以上的狗和booster= false放入狗列表中
导入java.util.linkedList;
导入java.util.List;
导入java.util.Scanner;
public class String {
}
vh0rcniy3#
}
hi3rlvi24#
一种可能性:
a.setBoosterShot(true);