Java面向对象程序设计基础实战

Java面向对象程序设计基础实战

一 函数题

6-1 设计一个矩形类Rectangle

设计一个名为Rectangle的类表示矩形。这个类包括:
两个名为width和height的double型数据域,它们分别表示矩形的宽和高。width和height的默认值都为1.
一个无参构造方法。
一个为width和height指定值的矩形构造方法。
一个名为getArea()的方法返回这个矩形的面积。
一个名为getPerimeter()的方法返回这个矩形的周长。

类名为:

Rectangle
裁判测试程序样例:

import java.util.Scanner;
/* 你的代码将被嵌入到这里 */

public class Main {
  public static void main(String[] args) {
    Scanner input = new Scanner(System.in);

    double w = input.nextDouble();
    double h = input.nextDouble();
    Rectangle myRectangle = new Rectangle(w, h);
    System.out.println(myRectangle.getArea());
    System.out.println(myRectangle.getPerimeter());

    input.close();
  }
}

输入样例:

3.14  2.78

输出样例:

8.7292
11.84
class Rectangle{
    double width,height;
    Rectangle()
    {
        width = 1;
        height = 1;
    }
    Rectangle(double w,double h)
    {
        width = w;
        height = h;
    }
    double getArea()
    {
        return width * height;
    }
    double getPerimeter()
    {
        return (width + height) * 2;
    }
}

6-2 创建一个直角三角形类实现IShape接口

创建一个直角三角形类(regular triangle)RTriangle类,实现下列接口IShape。两条直角边长作为RTriangle类的私有成员,类中包含参数为直角边的构造方法。

interface IShape {// 接口
public abstract double getArea(); // 抽象方法 求面积
public abstract double getPerimeter(); // 抽象方法 求周长
}

###直角三角形类的定义:

直角三角形类的构造函数原型如下:

RTriangle(double a, double b);
其中 a 和 b 都是直角三角形的两条直角边。

裁判测试程序样例:

import java.util.Scanner;
import java.text.DecimalFormat;
 
interface IShape {
    public abstract double getArea();
 
    public abstract double getPerimeter();
}
 
/*你写的代码将嵌入到这里*/


public class Main {
    public static void main(String[] args) {
        DecimalFormat d = new DecimalFormat("#.####");
        Scanner input = new Scanner(System.in);
        double a = input.nextDouble();
        double b = input.nextDouble();
        IShape r = new RTriangle(a, b);
        System.out.println(d.format(r.getArea()));
        System.out.println(d.format(r.getPerimeter()));
        input.close();
    }
}

输入样例:

3.1 4.2

输出样例:

6.51
12.5202
class RTriangle implements IShape{
    private double a;
    private double b;
    RTriangle(double a, double b){
        super();
        this.a = a;
        this.b = b;
    }
    public double getArea() {
        return this.a * this.b / 2.0;
    }
    public double getPerimeter() {
        return this.a + this.b + Math.sqrt(a * a + b * b);
    }
}

二 编程题

7-1 jmu-Java-03面向对象基础-01-构造函数与toString

定义一个有关人的Person类,内含属性:
String name、int age、boolean gender、int id,所有的变量必须为私有(private)。
注意:属性顺序请严格按照上述顺序依次出现。

1.编写无参构造函数:
打印"This is constructor"。
将name,age,gender,id按照name,age,gender,id格式输出

2.编写有参构造函数
依次对name,age,gender赋值。

3.覆盖toString函数:
按照格式:类名 [name=, age=, gender=, id=]输出。建议使用Eclipse自动生成.

4.对每个属性生成setter/getter方法

5.main方法中
首先从屏幕读取n,代表要创建的对象个数。
然后输入n行name age gender , 调用上面2编写的有参构造函数新建对象。
然后将刚才创建的所有对象逆序输出。
接下来使用无参构造函数新建一个Person对象,并直接打印该对象。

输入样例:

3
a 11 false
b 12 true
c 10 false

输出样例:

Person [name=c, age=10, gender=false, id=0]
Person [name=b, age=12, gender=true, id=0]
Person [name=a, age=11, gender=false, id=0]
This is constructor
null,0,false,0
Person [name=null, age=0, gender=false, id=0]
import java.util.Scanner;

public class Main
{
    public static void main(String[] args)
    {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        Person f[] = new Person[n];
        for(int i=0;i<n;i++)
        {
            String name = in.next();
            int age = in.nextInt();
            boolean gender = in.nextBoolean();
            f[i] = new Person(name,age,gender);
        }
        for(int i=n-1;i>=0;i--)
            f[i].toString();  // 调用toString方法
        f.toString();
        Person P = new Person();  // 调用无参数方法
    }
}
class Person
{
    private String name = null;
    private int age = 0,id = 0;
    private boolean gender = false;
    public Person()
    {
        System.out.println("This is constructor");
        System.out.println(name+","+age+","+gender+","+id);
        System.out.println("Person [name="+name+", age="+age+", gender="+gender+", id="+id+"]");
    }
    public Person(String name,int age,boolean gender)
    {
        this.name = name;
        this.age = age;
        this.gender = gender;
    }
    public String toString()
    {
        System.out.println("Person [name="+this.name+", age="+this.age+", gender="+this.gender+", id="+0+"]");
        return name;
    }
}

7-2 jmu-Java-03面向对象基础-02-构造函数与初始化块

1.定义一个Person类
属性:String name, boolean gender, int age, int id ,所有的变量必须为私有(private)。
无参构造函数:Person(), 功能:打印This is constructor 。
有参构造函数:Person(name, gender, age) ,功能:给属性赋值。
建议:使用Eclipse自动生成toString方法

2.定义类的初始化块
为Person类加入初始化块,在初始化块中对id属性赋值,并且要保证每次的值比上次创建的对象的值+1。然后在下一行打印This is initialization block, id is … 其中…是id的值。
提示:可为Person类定义一个static属性来记录所创建的对象个数。

3.编写静态初始化块
打印This is static initialization block

4.编写main方法
首先输入n,代表要创建的对象数量。
然后从控制台分别读取n行的name, gender, age, 并调用有参构造函数Person(name, age, gender)新建对象 。
将创建好的n个对象逆序输出(即输出toString()方法)。
使用无参构造函数新建一个Person对象,然后直接打印该对象。

思考
初始化类与对象有几种方法,构造函数、初始化块、静态初始化块。这三种方法执行的先后顺序是什么?各执行几次。

解析:

输入样例:先后顺序分别是静态初始-》初始化块-》构造函数,执行次数为1次,n次,n次

3
a 11 false
b 12 true
c 10 false

输出样例:

This is static initialization block
This is initialization block, id is 0
This is initialization block, id is 1
This is initialization block, id is 2
Person [name=c, age=10, gender=false, id=2]
Person [name=b, age=12, gender=true, id=1]
Person [name=a, age=11, gender=false, id=0]
This is initialization block, id is 3
This is constructor
null,0,false,3
Person [name=null, age=0, gender=false, id=3]
import java.util.Scanner;

public class Main
{
    public static void main(String[] args)
    {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        Person f[] = new Person[n];
        for(int i=0;i<n;i++)
        {
            String name = in.next();
            int age = in.nextInt();
            boolean gender = in.nextBoolean();
            f[i] = new Person(name,age,gender);
            System.out.println("This is initialization block, id is "+i);
        }
        for(int i=n-1;i>=0;i--)
            f[i].toString();
        f.toString();
        Person P = new Person();  // 调用Person方法
    }
}
class Person
{
    static
    {
        System.out.println("This is static initialization block");
    }
    private String name = null;
    private int age = 0,id = 0;
    private boolean gender = false;
    private static int num = 0;
    public Person(String name,int age,boolean gender)
    {
        this.name = name;
        this.age = age;
        this.gender = gender;
        id = num ++;
    }
    public Person()
    {
        System.out.println("This is initialization block, id is "+num);
        System.out.println("This is constructor");
        System.out.println(name+","+age+","+gender+","+num);
        System.out.println("Person [name="+name+", age="+age+", gender="+gender+", id="+num+"]");
    }
    public String toString()
    {
        System.out.println("Person [name="+this.name+", age="+this.age+", gender="+this.gender+", id="+id+"]");
        return name;
    }
}

7-3 jmu-Java-03面向对象基础-03-形状 (3 分)

  1. 定义长方形类与圆形类Circle
    长方形类-类名:Rectangle,private属性:int width,length
    圆形类-类名:Circle,private属性:int radius

编写构造函数:
带参构造函数:Rectangle(width, length),Circle(radius)

编写方法:
public int getPerimeter(),求周长。
public int getArea(),求面积。
toString方法,使用Eclipse自动生成。

注意:

计算圆形的面积与周长,使用Math.PI。
求周长和面积时,应先计算出其值(带小数位),然后强制转换为int再返回。
2. main方法
输入2行长与宽,创建两个Rectangle对象放入相应的数组。
输入2行半径,创建两个Circle对象放入相应的数组。
输出1:上面2个数组中的所有对象的周长加总。
输出2:上面2个数组中的所有对象的面积加总。
最后需使用Arrays.deepToString分别输出上面建立的Rectangle数组与Circle数组
思考:如果初次做该题会发现代码冗余严重。使用继承、多态思想可以大幅简化上述代码。

输入样例:

1 2
3 4
7
1

输出样例:

69
170
[Rectangle [width=1, length=2], Rectangle [width=3, length=4]]
[Circle [radius=7], Circle [radius=1]]
import java.util.*;
import java.lang.*;

class Rectangle
{
    private int width;
    private int length;
    Rectangle(int width,int length)
    {
        this.width = width;
        this.length = length;
    }
    public int getPerimeter()
    {
        return (width + length) * 2;
    }
    public int getArea()
    {
        return width * length;
    }
    @Override
    public String toString()
    {
        return "Rectangle [width="+width+", length="+length+"]";
    }
}
class Circle
{
    private int radius;
    Circle(int radius)
    {
        this.radius = radius;
    }
    public int getPerimeter()
    {
        return (int)(2 * Math.PI * radius);
    }
    public int getArea()
    {
        return (int)(Math.PI * radius * radius);
    }
    @Override
    public String toString()
    {
        return "Circle [radius="+radius+"]";
    }
}
public class Main
{
    public static void main(String[] args)
    {
        Scanner in = new Scanner(System.in);
        Rectangle rect[] = new Rectangle[2];
        Circle cir[] = new Circle[2];
        for(int i=0;i<rect.length;i++)
            rect[i] = new Rectangle(in.nextInt(),in.nextInt());
        for(int i=0;i<cir.length;i++)
            cir[i] = new Circle(in.nextInt());
        int x = rect[0].getPerimeter() + rect[1].getPerimeter();
        int y = cir[0].getPerimeter() + cir[1].getPerimeter();
        System.out.println(x + y);
        x = rect[0].getArea() + rect[1].getArea();
        y = cir[0].getArea() + cir[1].getArea();
        System.out.println(x + y);
          // 使用Arrays.deepToString分别输出上面建立的Rectangle数组与Circle数组
		System.out.println(Arrays.deepToString(rect));
		System.out.println(Arrays.deepToString(cir));
    }
}
  • 2
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值