小弟刚学JAVA没多久,看到书上的问题不懂~~向各位高手指教,小弟万分感激~(300分)

  • 主题发起人 主题发起人 启吾
  • 开始时间 开始时间

启吾

Unregistered / Unconfirmed
GUEST, unregistred user!
JAVA题目:
1、设有一个Rectangle类,该类是矩形的蓝图,具有:
属性: 宽属性:private int width
高属性:private int height
方法: 周长:public int girth()
面积: public int area()
构造方法: 用于设置宽属性和高属性。
Public Rectangle(int width, int height)
请创建该类。
2、编写程序,计算1个宽为10,高为8的矩形的周长和面积。
3、如果要求Rectangle类的属性(宽和高)在初始化后能被改变,可以采取什么方法。请用2种以上的方法实现,并说明其异同及优缺点。

4、设有1个Member类,该类是人的属性的蓝图,具有:
属性: 姓名属性:protected String name
性别属性:protected Boolean sex(false代表女)
年龄属性:protected int age
方法: 详细信息:public String getDetails(),返回所有属性。
构造方法: 用于设置姓名,性别和年龄。
Public Member(String name, boolean sex, int age)
Public Member(String name, int age), 该构造方法设置性别为女,要求调用第1个构造方法实现。(this())。
//注意构造方法的overload重载。
请创建该类。
5、编写程序,输出1个姓名为John,年龄28的先生的信息;输出1个姓名为Rose,年龄为18的女士的信息(要求采用第2个构造方法)。

6、设有1个Teacher类,继承自Member类,该类是教师的蓝图,它还具有:
属性: 讲授课程属性:protected String course
方法: 详细信息:public String getDetails(),返回所有属性。
构造方法: 用于设置姓名,性别和年龄和讲授课程。
Public Teacher(String name, boolean sex, int age, String course),要求调用super()方式实现。
Public Teacher(String name, int age,String course), 该构造方法设置性别为女,要求调用super()方式实现。
//注意构造方法的overriding重写。
请创建该类。

7、编写程序,输出1个姓名为John,年龄28的,讲授“网络编程”的男老师信息;输出1个姓名为Rose,年龄为25的,讲授“English”的女老师的信息(要求采用第2个构造方法)。

8、设有1个Director类,继承自Teacher类,该类是专业主任的蓝图,它还具有:
属性: 专业属性:private String department
方法: 详细信息:public String getDetails(),返回所有属性。
构造方法: 用于设置姓名,性别和年龄讲授课程和部门。
Public Director(String name, boolean sex, int age, String course, String department),要求调用super()方式实现。
Public Teacher(String name, int age, String course, String department), 该构造方法设置性别为女,要求调用super()方式实现。
//注意构造方法的overriding重写以及super()的多级调用。
请创建该类。

9、编写程序,输出1个姓名为John,年龄28的,讲授“网络编程”的计算机专业主任(男)信息;输出1个姓名为Rose,年龄为25的,讲授“English”的专业主任(女)的信息(要求采用第2个构造方法)。
10、编写程序,打印1-9之间的偶数。
11、从键盘接受若干字符(最多16个),若为C,显示“继续”,若为b,结束输入。
12、随机生成2个0-1000之间的数,比较2个数的大小,并由大到小输出。
(1)用一个程序完成。RandomDemo.java
(2 )改写该程序,要求:
定义一个RandomDemo1类,该类拥有如下的成员。
属性:private int number1,number2
构造方法:public RandomDemo1(int number1,number2)
方法:public int return_max(),该方法返回大值。
public int return_min(),该方法返回小值。
再编写一个程序,该程序用来测试RandomDemo1类,该程序的功能是:
该程序有2个int类型的变量number1,number2,它们的值随机生成,在0~1000之间;
定义1个RandomDemo1实例对象ran_num,将其属性初始化number1和number2;
按大小打印ran_num的属性
谢谢各位高手帮忙~~
 
1.
public class Rectangle{
private int width;
private int height;
public int getWidth(){ // property width
return width;
}
public setWidth(int value){
width = value;
}
public int getHeight(){ // property height
return height;
}
public setHeight(value){
width = value;
}
public int area(){ // get Rectangle area
return width * height;
}
public static void Rectangle(){ // construction
width = 1;
height = 1;
}
}
2.
public class myRectangle{
public int getLength(int width;
int height){
return (width + height) * 2;
}
public int getArea(int width;
int height){
return width * height;
}
}
3. 在1中已经实现1种方法
4.
class member{
protected String name;
protected Boolean sex;
protected int age;
public String getDetails(){
return "name:" + name + ";sex" + sex.toString() + ";age:" + (String)age;
}
public static member(String aName;
boolean aSex;
int anAge){
name = aName;
sex = aSex;
age = anAge;
}
public member(String name, int age){
member(name, age, false);
}
}
5.
member m = new member("john", true, 28);
member m2 = new member("rose", 18);
system.writeln(m.getDetail());
system.writeln(m2.getDetail());
m.
6.
public class teacher extend member{
protected String course;
public override String getDetails(){
return super.getDetails() + ";course:" + course;
}
public static teacher(String name;
boolean sex;
int age;
String course){
super(name, sex, age);
this.course = course;
}
public teacher(String name, int age, String course){
super(name, false, int);
this.course = course;
}
}
7.
teacher t = new teacher("john", 28, "网络编程");
system.writeln(t.getDetail());
teacher t2 = new teacher("rose", false, 25);
course = "English";
system.writeln(t2.getDetail());
8.
class director extend teacher{
private String department;
public String getDetails(){
return super.getDetails() + ";dept:" + department;
}
public static director(String name;
boolean sex;
int age;
String course;
String department){
this.name = name;
this.sex = sex;
this.course = course;
this.department = department;
}
public director(String name, int age, String course, String department){
super(name, age, course);
this.sex = false;
this.department = department;
}
10.
for (int i = 1;
i <= 9;
i++)
if (i / 2 == 0) system.writeln(i);
 
我也刚学的JAVA,没事就写写了!
真是太多了....我眼睛都花了.... :(
 
前3个:
1。
class BaseRectangle{
private int width;
private int height;
public int area(){
return width * height;
}

public int girth(){
return (width + height) * 2;
}

public BaseRectangle(int iWidth, int iHeight){
width = iWidth;
height = iHeight;
}
}
2.
class TestRectangle{
public static void main(String[] args){
BaseRectangle rect = new BaseRectangle(10, 8);
System.out.println("Area is: " + rect.area());
}
}
3. RectangleA 和 RectangleB 都实现了相同的功能,但是 RectangleA 封装了对成员
的访问,RectangleB 对成员直接访问。
class RectangleA extends BaseRectangle{
private int width;
private int height;
public RectangleA(){
super(0, 0);
}

public int getWidth(){
return width;
}

public void setWidth(int iWidth){
width = iWidth;
}

public int getHeight(){
return height;
}

public void setHeight(int iHeight){
height = iHeight;
}
}
class RectangleB extends BaseRectangle{
public int width;
public int height;
public RectangleB(){
super(0, 0);
}
}
class MyRectangle{
public static void main(String[] args){
RectangleA a = new RectangleA();
RectangleB b = new RectangleB();

System.out.println("(Original) a.width, b.width is: " + a.getWidth() + ", " + b.width);
a.setWidth(30);
b.width = 30;
System.out.println("(Modified) a.width, b.width is: " + a.getWidth() + ", " + b.width);
}
}
 
4-9:
4.
class BaseMember{
protected String name;
protected boolean sex;
protected int age;
public BaseMember(String sName, boolean bSex, int iAge){
name = sName;
sex = bSex;
age = iAge;
}

public BaseMember(String name, int age){
this(name, false, age);
}

public String getDetails(){
return "Name: " + name + ", Sex: " + (sex ? "male" : "female") + ", Age: " + age;
}
}
5.
class TestMember{
public static void main(String[] args){
BaseMember male = new BaseMember("John", true, 28);
System.out.println(male.getDetails());
BaseMember female = new BaseMember("Rose", 18);
System.out.println(female.getDetails());
}
}
6.
class Teacher extends BaseMember{
protected String course;
public Teacher(String sName, boolean bSex, int iAge, String sCourse){
super(sName, bSex, iAge);
course = sCourse;
}

public Teacher(String sName, int iAge, String sCourse){
super(sName, iAge);
course = sCourse;
// this(sName, false, iAge, course);
}

public String getDetails(){
return super.getDetails() + ", Course: " + course;
}
}
7.
class TestTeacher{
public static void main(String[] args){
Teacher john = new Teacher("John", true, 28, "网络编程");
Teacher rose = new Teacher("Rose", 25, "English");
System.out.println("Teacher 1: " + john.getDetails());
System.out.println("Teacher 2: " + rose.getDetails());
}
}
8.
class Director extends Teacher{
private String department;
public Director(String sName, boolean bSex, int iAge, String sCourse, String sDepartment){
super(sName, bSex, iAge, sCourse);
department = sDepartment;
}

public Director(String sName, int iAge, String sCourse, String sDepartment){
super(sName, iAge, sCourse);
department = sDepartment;
// this(sName, false, iAge, sCourse, sDepartment);
}

public String getDetails(){
return super.getDetails() + ", Department: " + department;
}
}
9.
class TestDirector{
public static void main(String[] args){
Director john = new Director("John", true, 28, "计算机", "专业主任");
Director rose = new Director("Rose", 25, "English", "专业主任");
System.out.println("Director 1: " + john.getDetails());
System.out.println("Director 2: " + rose.getDetails());
}
}
 
10.
class TestOddNumber{
public static void main(String[] args){
String strResult = "Result: ";
for (int i = 0;
i < 10;
++i){
if (i % 2 == 0) strResult += i + " ";
}

System.out.println(strResult);
}
}
11. 这个方法的实现不好,输入一个字符要敲一下回车,不知道怎么解决? [:(]
class TestKbInput{
public static void main(String[] args){
char a;
try {
for (int i = 0;
i < 32;
++i){
a = (char)System.in.read();
if (a == 'C') System.out.print("Continue");
else
if (a == 'b') break;
}
} catch (Exception e){ e.printStackTrace();
}
}
}
 
不会吧,都把作业搬上来了 ?
自己试着写吧,出错了再问,否则……………………!
 
Java原来不支持属性啊?
 
12.
(1)RandomDemo.java
class RandomDemo{
public static void sort(double[] arr){
java.util.Arrays.sort(arr);
}

public static void main(String[] args){
double[] a = newdo
uble[2];
a[0] = Math.random() * 1000;
a[1] = Math.random() * 1000;

sort(a);
System.out.println("Sorted ramdom numbers: " + a[1] + ", " + a[0]);
}
}
(2)
class RandomDemo1{
private int number1, number2;
public RandomDemo1(int num1, int num2){
number1 = num1;
number2 = num2;
}

public int return_max(){
return (number1 > number2 ? number1 : number2);
}

public int return_min(){
return (number1 > number2 ? number2 : number1);
}
}
class TestRandomDemo1{
public static void main(String[] args){
int number1, number2;
number1 = (int)Math.round(Math.random() * 1000);
number2 = (int)Math.round(Math.random() * 1000);
RandomDemo1 ran_num = new RandomDemo1(number1, number2);

System.out.println("Max: " + ran_num.return_max() + ", Min: " + ran_num.return_min());
}
}
 
其实我也是刚学没多久,缺乏练习。你也是应该多做练习,不然基础打不好。
 
时间紧迫,只好出此下策,请各位高手原谅!!
 
后退
顶部