作业帮 > 综合 > 作业

英文java题Question 3.(a)Write a Java definition for a class Per

来源:学生作业帮 编辑:大师作文网作业帮 分类:综合作业 时间:2024/09/28 17:26:54
英文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.
英文java题Question 3.(a)Write a Java definition for a class Per
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("李四");
}
}
再问: 报错。。 说没有定义person
再答: 所有代码写在 TestPerson.java中 即可