java题目 ,那位高手帮忙做一下,谢谢了 Java题目,高手麻烦帮忙做一下!
public class Dog {
private String name;
private String color;
private int weight;
private int age;
public Dog() {
}
public Dog(String name, String color, int weight, int age) {
this.name = name;
this.color = color;
this.weight = weight;
this.age = age;
}
public String toString() {
String returnStr = "Dog name:" + this.name + " Dog color:" + this.color
+ " Dog weight:" + this.weight + " Dog age" + this.age;
return returnStr;
}
public void shoot() {
System.out.println("This is " + this.name +" shoot!");
}
public void bite() {
System.out.println("This is " + this.name + " bite");
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public int getWeight() {
return weight;
}
public void setWeight(int weight) {
this.weight = weight;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public static void main(String[] args) {
Dog dog1 = new Dog("Dog1", "white", 5, 4);
Dog dog2 = new Dog("Dog2", "red", 6 , 4);
dog1.shoot();
dog2.bite();
}
}
已经试验过了
package dog;
public class Dog { private String dogName; private String dogColor; private int dogAge;
public Dog() {
}
public Dog(String name, String color, int age) { dogName = name; dogColor = color; dogAge = age; }
private void Away() { System.out.println("汪汪"); }
private void Bite() { System.out.println("撕咬"); }
public String toString() { return "名字:" + this.dogName + "颜色:" + this.dogColor + "年龄:" + this.dogAge; }
public static void main(String[] args) { Dog dog1 = new Dog("哈巴狗", "棕色", 1); System.out.println(dog1.toString()); dog1.Away();
Dog dog2 = new Dog("牧羊犬", "黑色", 1); System.out.println(dog1.toString()); dog2.Bite(); }
}
run结果:
名字:哈巴狗颜色:棕色年龄:1汪汪名字:哈巴狗颜色:棕色年龄:1撕咬
public class Dog
{
private String name;
private String color;
private int age;
private double body;
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
public String getColor()
{
return color;
}
public void setColor(String color)
{
this.color = color;
}
public int getAge()
{
return age;
}
public void setAge(int age)
{
this.age = age;
}
public double getBody()
{
return body;
}
public void setBody(double body)
{
this.body = body;
}
public Dog()
{
System.out.println("dog is coming");
}
public Dog(String name, String color, int age, double body)
{
super();
this.name = name;
this.color = color;
this.age = age;
this.body = body;
}
public String toString()
{
return this.getName()+":"+this.getAge();
}
public void yao()
{
System.out.print(this.getName()+"咬了");
}
public void jiao()
{
System.out.print(this.getName()+"叫了");
}
public final static void main()
{
Dog d1 = new Dog();
d1.setName("狗1");
d1.yao();
Dog d2 = new Dog();
d2.setName("狗2");
d2.jiao();
}
}
java题目,那位高手帮忙做一下,谢谢了~
第6问的文字问题描述不是很理解。稍微写了下。
public class Student {
//无参构造函数
public Student(){}
//有参构造函数
public Student(String no, String name, int age){
this.no = no;
this.age = age;
this.name = name;
}
private String no;//学号
private String name;//名称
private int age;//年龄
//-----------------------------Set 跟 Get 方法 start ------------------
public String getNo() {
return no;
}
public void setNo(String no) {
this.no = no;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
//-----------------------------Set 跟 Get 方法 end ------------------
//重写toString方法
@Override
public String toString() {
return "我的名字叫:".concat(name).concat(",学号是:").concat(no).concat(",今年").concat(age+"岁了!");
}
//计算学生总数的方法(大概多此一举)
public static int getCount(List list){
return list.size();
}
//程序入口
public static void main(String[] args) {
//保存多个学生对象的集合
List list = new ArrayList();
//学生对象1
Student stu = new Student();
stu.setAge(18);
stu.setName("小明");
stu.setNo("00001");
System.out.println(stu.toString());
list.add(stu);
//学生对象2
stu = new Student("00002", "小花", 22);
System.out.println(stu.toString());
list.add(stu);
System.out.println("共有学生人数:"+getCount(list)+"人");//输出学生对象集合总数
}
}
public interface Computer {
public void poweron();
}
class PC implements Computer{
public void poweron() {
System.out.println("PC实现Computer接口");
}
}
class Laptop implements Computer{
public void poweron() {
stem.out.println("Laptop实现Computer接口");
}
}
public class Test {
public static void main(String[] args) {
Computer pc=new PC();
Computer laptop=new Laptop();
pc.poweron();
laptop.poweron();
}
}