我是一名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
。
非常感谢任何帮助。
答案 0 :(得分:1)
你可以用它。试着询问狗是否是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");
}
答案 1 :(得分:0)
尝试放置For循环。循环五次然后将所有2年以上的狗和booster = false放入狗列表中
import java.util.LinkedList;
import java.util.List;
import java.util.Scanner;
公共类示例{
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());
}
}
}
答案 2 :(得分:-1)
一种可能性:
a.setBoosterShot(真);