英文java题Question 3.(a)Write a Java definition for a class Person defined byname The name of the personage The person’s ageheight The person’s height (in meters)(you need to decide what is the type of each attribute).The class should contain

来源:学生作业学帮网 编辑:学帮网 时间:2024/05/07 14:44:49

英文java题
Question 3.
(a)
Write a Java definition for a class Person defined by
name The name of the person
age The person’s age
height The person’s height (in meters)
(you need to decide what is the type of each attribute).
The class should contain two methods:a constructor (to set up an instance),and
a method greetings which displays the message
“Hello,my name is
name
,I am
age
,and I am
height
meters tall.”
(b) Write a Java application program that instantiates an object in the class Person
and then calls the greetings method.
(c) Define another greetings method which will print the message
“Hello your_name
,my name is name
,I am age
,and I am height meters tall.” (assume that your name is defined as a constant string in the main
method).
(d) Suppose we want to give this person an additional skill.She must be able to
convert a decimal length measure given in centimeters (to be read from the input)
to the imperial equivalent in inches.
Note:
use Person and TestPerson (driver class) for the two classes.You only
need to implement all the required methods in the one Person class.Extra
methods will be ignored.

class Person {
private String name ;
private int age ;
private int height;
public Person(String name,int age ,int height){
this.name = name ;
this.age = age ;
this.height = height ;
}
public void setName(String name){
this.name = name ;
}
public void setAge(int age){
this.age = age ;
}
public void setHeight(int height){
this.height = height ;
}
public void greetings(){
System.out.println("Hello,my name is"+name+",I am "+age+",and I am "+height+"meters tall.");
}
public void greetings(String yourname){
System.out.println("Hello,"+yourname+" my name is"+name+",I am "+age+",and I am "+height+"meters tall.");
}
}
public class TestPerson{
public static void main(String[] args){
Person p = new Person("张三",18,175);
p.greetings();
p.greetings("李四");
}
}