私はJavaプログラミングの学生であり、割り当てが忙しいです。私は本当にいくつかの助けに感謝します。以下は 犬のクラス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
です。
ご協力いただきますようお願い申し上げます。
インデントコードは、次のプログラミング過程で一部のだろうか? – Tom
私の謝罪tom –