JAVA编程题
四、编程题(20分)1、请编写一个Application,其功能为:在程序中取两个随机整数,以如下形式显示它们的和(设两个随机数分别为769,857):769+857=1...
四、编程题(20分)
1、请编写一个Application,其功能为:在程序中取两个随机整数,以如下形式显示它们的和(设两个随机数分别为769,857):
769 + 857 = 1626
2、定义一个你所熟悉的类,再设计它的一个子类。编写一个应用程序,输出该子类的相关数据(属性值)。要求:每个类中都要定义构造函数和一个myPrint()方法,子类中的myPrint()方法要覆盖父类该方法。
各位大哥,谁会啊?帮帮忙 展开
1、请编写一个Application,其功能为:在程序中取两个随机整数,以如下形式显示它们的和(设两个随机数分别为769,857):
769 + 857 = 1626
2、定义一个你所熟悉的类,再设计它的一个子类。编写一个应用程序,输出该子类的相关数据(属性值)。要求:每个类中都要定义构造函数和一个myPrint()方法,子类中的myPrint()方法要覆盖父类该方法。
各位大哥,谁会啊?帮帮忙 展开
7个回答
展开全部
我会,但没分
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
第一道题:
import java.util.Random;
public class RandomAdd
{
int random1,random2;
public RandomAdd()
{
Random ran = new Random();
random1 = ran.nextInt(1000);
random2 = ran.nextInt(1000);
}
public static void main(String[] args)
{
RandomAdd r = new RandomAdd();
System.out.println(r.random1+"+"+r.random2+"="+(r.random1+r.random2));
}
}
第二道题:
class Person
{
String name;
int age;
public Person(){}
public Person(String name,int age)
{
this.name = name;
this.age = age;
}
public void myPrint()
{
System.out.println("Person name:"+name+"\t"+"Person age:"+age);
}
}
class Student extends Person
{
String school;
public Student(){}
public Student(String name,int age,String school)
{
super(name,age);
this.school = school;
}
public void myPrint()
{
System.out.println("Student name:"+name+"\t"+"Student age:"+age+"\t"+"Student school:"+school);
}
}
public class ExtendsExample
{
public static void main(String[] args)
{
Person p = new Person("zhangsan",20);
p.myPrint();
Student s = new Student("yangyan",21,"swjtu");
s.myPrint();
}
}
import java.util.Random;
public class RandomAdd
{
int random1,random2;
public RandomAdd()
{
Random ran = new Random();
random1 = ran.nextInt(1000);
random2 = ran.nextInt(1000);
}
public static void main(String[] args)
{
RandomAdd r = new RandomAdd();
System.out.println(r.random1+"+"+r.random2+"="+(r.random1+r.random2));
}
}
第二道题:
class Person
{
String name;
int age;
public Person(){}
public Person(String name,int age)
{
this.name = name;
this.age = age;
}
public void myPrint()
{
System.out.println("Person name:"+name+"\t"+"Person age:"+age);
}
}
class Student extends Person
{
String school;
public Student(){}
public Student(String name,int age,String school)
{
super(name,age);
this.school = school;
}
public void myPrint()
{
System.out.println("Student name:"+name+"\t"+"Student age:"+age+"\t"+"Student school:"+school);
}
}
public class ExtendsExample
{
public static void main(String[] args)
{
Person p = new Person("zhangsan",20);
p.myPrint();
Student s = new Student("yangyan",21,"swjtu");
s.myPrint();
}
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询