public class Pet { String Name; String Type; int Age; int Height; int Weight; in
ID: 3858091 • Letter: P
Question
public class Pet {
String Name;
String Type;
int Age;
int Height;
int Weight;
int Legs;
public String getName() {
return Name;
}
public void setName(String name) {
Name = name;
}
public String getType() {
return Type;
}
public void setType(String type) {
Type = type;
}
public int getAge() {
return Age;
}
public void setAge(int age) {
Age = age;
}
public int getHeight() {
return Height;
}
public void setHeight(int height) {
Height = height;
}
public int getWeight() {
return Weight;
}
public void setWeight(int weight) {
Weight = weight;
}
public int getLegs() {
return Legs;
}
public void setLegs(int legs) {
Legs = legs;
}
public void PrintStory(String TYPE,String NAME,int AGE,int HEIGHT,int WEIGHT,int LEGS){
System.out.println("There once was a "+TYPE+" named "+NAME+" who lived in Dallas, Texas. "+NAME+" was a "+LEGS+" legged pet."+
" In 2017, when it was "+AGE+" years old, "+NAME+" was "+HEIGHT+" feet tall and "+
WEIGHT+" pounds. "+NAME+" was a happy "+TYPE+" and had long and happy life.");
}
public static void main(String[] args){
Pet petObject=new Pet();
String name="Bruno";
String type="Pug";
int weight=14;
int legs=4;
int height=5;
int age=3;
petObject.setName(name);
petObject.setHeight(height);
petObject.setHeight(height);
petObject.setType(type);
petObject.setLegs(legs);
petObject.setWeight(weight);
petObject.setAge(age);
String NAME=petObject.getName();
int AGE=petObject.getAge();
String TYPE=petObject.getType();
int WEIGHT=petObject.getWeight();
int LEGS=petObject.getLegs();
int HEIGHT=petObject.getHeight();
petObject.PrintStory(TYPE, NAME, AGE, HEIGHT, WEIGHT, LEGS);
}
}
Need to modify this code so that the fields (name, type, age, weight, height and legs) are values entered by the user.
Explanation / Answer
Modified code is as follows:
import java.util.*;
public class Pet {
String Name;
String Type;
int Age;
int Height;
int Weight;
int Legs;
public String getName() {
return Name;
}
public void setName(String name) {
Name = name;
}
public String getType() {
return Type;
}
public void setType(String type) {
Type = type;
}
public int getAge() {
return Age;
}
public void setAge(int age) {
Age = age;
}
public int getHeight() {
return Height;
}
public void setHeight(int height) {
Height = height;
}
public int getWeight() {
return Weight;
}
public void setWeight(int weight) {
Weight = weight;
}
public int getLegs() {
return Legs;
}
public void setLegs(int legs) {
Legs = legs;
}
public void PrintStory(String TYPE,String NAME,int AGE,int HEIGHT,int WEIGHT,int LEGS){
System.out.println("There once was a "+TYPE+" named "+NAME+" who lived in Dallas, Texas. "+NAME+" was a "+LEGS+" legged pet."+
" In 2017, when it was "+AGE+" years old, "+NAME+" was "+HEIGHT+" feet tall and "+
WEIGHT+" pounds. "+NAME+" was a happy "+TYPE+" and had long and happy life.");
}
public static void main(String[] args){
Pet petObject=new Pet();
Scanner sc=new Scanner(System.in);//Scanner object to take user input
//Now we will take user input for name,type,weight,legs,height and age using scanner object
String name=sc.next();
String type=sc.next();
int weight=sc.nextInt();
int legs=sc.nextInt();
int height=sc.nextInt();
int age=sc.nextInt();
petObject.setName(name);
petObject.setHeight(height);
petObject.setHeight(height);
petObject.setType(type);
petObject.setLegs(legs);
petObject.setWeight(weight);
petObject.setAge(age);
String NAME=petObject.getName();
int AGE=petObject.getAge();
String TYPE=petObject.getType();
int WEIGHT=petObject.getWeight();
int LEGS=petObject.getLegs();
int HEIGHT=petObject.getHeight();
petObject.PrintStory(TYPE, NAME, AGE, HEIGHT, WEIGHT, LEGS);
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.