**
7-1 程序填空题3 (15 分)
**
参照输出样例补全以下程序,使程序输出结果与输出样例一致。
public class Main {
public static void main(String[] args) {
Son son = new Son();
son.method();
}
}
class Parent {
Parent() {
System.out.println(“Parent’s Constructor without parameter”);
}
Parent(boolean b) {
System.out.println("Parent's Constructor with a boolean parameter");
}
public void method() {
System.out.println("Parent's method()");
}
}
class Son extends Parent {
//补全本类定义
}
输入格式:
无
输出格式:
无
输入样例:
输出样例:
Parent’s Constructor with a boolean parameter
Son’s Constructor without parameter
Son’s method()
Parent’s method()
源代码:
import java.util.*;
public class Main {
public static void main(String[] args) {
Son son = new Son();//执行子类的构造方法
son.method();//子类的method方法
}
}
class Parent {
Parent() {
System.out.println("Parent's Constructor without parameter");
}
Parent(boolean b) {
System.out.println("Parent's Constructor with a boolean parameter");
}
public void method() {
System.out.println("Parent's method()");
}
}
class Son extends Parent {
private static boolean b;
Son(){
super(b);//子类构造方法重写执行父类带参构造方法,并输出
System.out.println("Son's Constructor without parameter");
}
public void method() {
System.out.println("Son's method()");
System.out.println("Parent's method()");
}
}
**
7-2 图形继承 (20 分)
**
编写程序,实现图形类的继承,并定义相应类对象并进行测试。
类Shape,无属性,有一个返回0.0的求图形面积的公有方法public double getArea();//求图形面积
类Circle,继承自Shape,有一个私有实型的属性radius(半径),重写父类继承来的求面积方法,求圆的面积
类Rectangle,继承自Shape,有两个私有实型属性width和length,重写父类继承来的求面积方法,求矩形的面积
类Ball,继承自Circle,其属性从父类继承,重写父类求面积方法,求球表面积,此外,定义一求球体积的方法public double getVolume();//求球体积
类Box,继承自Rectangle,除从父类继承的属性外,再定义一个属性height,重写父类继承来的求面积方法,求立方体表面积,此外,定义一求立方体体积的方法public double getVolume();//求立方体体积
注意:
每个类均有构造方法,且构造方法内必须输出如下内容:Constructing 类名
每个类属性均为私有,且必须有getter和setter方法(可用Eclipse自动生成)
输出的数值均保留两位小数
主方法内,主要实现四个功能(1-4): 从键盘输入1,则定义圆类,从键盘输入圆的半径后,主要输出圆的面积; 从键盘输入2,则定义矩形类,从键盘输入矩形的宽和长后,主要输出矩形的面积; 从键盘输入3,则定义球类,从键盘输入球的半径后,主要输出球的表面积和体积; 从键盘输入4,则定义立方体类,从键盘输入立方体的宽、长和高度后,主要输出立方体的表面积和体积;
假如数据输入非法(包括圆、矩形、球及立方体对象的属性不大于0和输入选择值非1-4),系统输出Wrong Format
输入格式:
共四种合法输入
1 圆半径
2 矩形宽、长
3 球半径
4 立方体宽、长、高
输出格式:
按照以上需求提示依次输出
输入样例1:
在这里给出一组输入。例如:
1 1.0
输出样例1:
在这里给出相应的输出。例如:
Constructing Shape
Constructing Circle
Circle’s area:3.14
输入样例2:
在这里给出一组输入。例如:
4 3.6 2.1 0.01211
输出样例2:
在这里给出相应的输出。例如:
Constructing Shape
Constructing Rectangle
Constructing Box
Box’s surface area:15.26
Box’s volume:0.09
输入样例3:
在这里给出一组输入。例如:
2 -2.3 5.110
输出样例2:
在这里给出相应的输出。例如:
Wrong Format
源代码:
import java.util.*;
class Shape{
public Shape(){
System.out.println("Constructing Shape");
}
public double getArea(){
return 0.0;
}
}
class Circle extends Shape{
double radius;
double pi = Math.PI;
public Circle(){
super();
System.out.println("Constructing Circle");
}
public double getArea(){
return pi*radius*radius;
}
public double getRadius(){
return radius;
}
public void setRadius(double radius){
this.radius=radius;
}
}
class Rectangle extends Shape{
double width;
double length;
public Rectangle(){
super();
System.out.println("Constructing Rectangle");
}
public double getArea(){
return width*length;
}
public double getWidth(){
return width;
}
public void setWidth(double width){
this.width=width;
}
public double getLength(){
return length;
}
public void setLength(double length){
this.length=length;
}
}
class Ball extends Circle{
public Ball(){
System.out.println("Constructing Ball");
}
public double getArea() {
return super.getArea()*4;
}
public void setRadius(double radius){
this.radius=radius;
}
public double getRadius(){
return radius;
}
public double getVolume(){
return 4*pi*radius*radius*radius/3;
}
}
class Box extends Rectangle{
double height;
public Box(){
super();
System.out.println("Constructing Box");
}
public double getHeight() {
return height;
}
public void setHeight(double height) {
this.height = height;
}
public double getLength(){
return length;
}
public void setLength(double length) {
super.setLength(length);
}
public double getWidth() {
return super.getWidth();
}
public void setWidth(double width) {
super.setWidth(width);
}
public double getArea() {
return 2*width*height+2*width*length+2*height*length;
}
public double getVolume(){
return width*length*height;
}
}
public class Main {
public static void main(String[] args){
Scanner sc =new Scanner(System.in);
double r ;
r = sc.nextDouble();
if(r==1){
double ra1=sc.nextDouble();
if(ra1>0){
Circle c1 = new Circle();
c1.setRadius(ra1);
double s;
s=c1.getArea();
System.out.printf("Circle's area:%.2f",s);
}
else System.out.printf("Wrong Format");
}
if(r==2){
double k=sc.nextDouble();
double c=sc.nextDouble();
if(k>0&&c>0){
Rectangle c2=new Rectangle();
c2.setWidth(k);
c2.setLength(c);
double b =c2.getArea();
System.out.printf("Rectangle's area:%.2f",b);
}
else System.out.printf("Wrong Format");
}
if(r==3){
double ra2 = sc.nextDouble();
if(ra2>0){
Ball c3 =new Ball();
c3.setRadius(ra2);
double d = c3.getArea();
double e = c3.getVolume();
System.out.printf("Ball's surface area:%.2f",d);
System.out.printf("Box's volume:%.2f",e);
}
else System.out.printf("Wrong Format");
}
if(r==4){
double k1 = sc.nextDouble();
double cc = sc.nextDouble();
double g = sc.nextDouble();
if(k1>0&&cc>0&&g>0){
Box c4 =new Box();
c4.setWidth(k1);
c4.setLength(cc);
c4.setHeight(g);
double n = c4.getArea();
double m = c4.getVolume();
System.out.printf("Box's surface area:%.2f\n",n);
System.out.printf("Box's volume:%.2f",m);
}
else System.out.printf("Wrong Format");
}
}
}
**
7-3 横平竖直 (15 分)
**
程序填空题。根据题目要求完善下面的代码。请提交完整代码。 一个木块如果高度比宽度大,我们说它是竖着放的,否则我们说它是平放的。 读入一个木块的高度和宽度。如果它是平放的,则输出A,否则输出B。
import java.util.Scanner;
public class Main{
public static void main(String[] args){
Scanner in = new Scanner(System.in);
int height, width;
char status;
height = in.nextInt();
width = in.nextInt();
Board board = new Board(height, width);
status = board.getStatus();
System.out.print(status);
}
}
class Board{
int height, width;
public Board(int height, int width){
this.height = height;
this.width = width;
}
public char getStatus(){
if(height<=width){
return status(1);
}else{
return status(1.0);
}
}
public char status(double rate){
}
public char status(int rate){
}
}
输入格式:
输入在一行中给出2个绝对值不超过1000的正整数A和B。
输出格式:
在一行中输出一个字符A或者B。
输入样例:
50 50
输出样例:
A
源代码:
import java.util.*;
class Board{
int height,width;
public Board(int height,int width){
this.height=height;
this.width=width;
}
}
public class Main {
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
int height= sc.nextInt();
int width= sc.nextInt();
char status;
Board d=new Board(height,width);
int a=d.height;
int b=d.width;
if(a>b){
System.out.println("B");
}
else System.out.println("A");
}
}
**
7-4 好玩的图形 (20 分)
**
给定图形接口定义如下: interface Shape{
float getArea();//求面积
float getPerimeter();//求周长
}
请实现以上接口,定义圆形类(用数字1表示)、矩形类(用数字2表示)。
从键盘输入图形类别(1代表圆,2代表矩形)和相应的参数,计算并输出相应图形的面积和周长,结果保留小数点后2位数据。
输入格式:
输入数据包含多行,第一行一个整数n,表示接下来共有n个图形对象需要生成。
每个图形数据占2行,第一行为数字1或2,表示图形类别,第二行为生成图形的参数。
输出格式:
每个图形对应的面积和周长。
输入样例:
2
1
1.0
2
1.0 2.0
输出样例:
3.14 6.28
2.00 6.00
源代码:
import java.util.*;
interface Shape {
public float getArea();
public float getPerimeter();
}
class Circle implements Shape {
float radius;
public Circle(float radius) {
this.radius = radius;
}
public float getArea() {
float ans = (float) (Math.PI * radius * radius);
return (float) ans;
}
public float getPerimeter() {
float ans = (float) (2 * Math.PI * radius);
return (float) ans;
}
}
class Rectangle implements Shape {
float length, width;
public Rectangle(float length, float width) {
this.length = length;
this.width = width;
}
public float getArea() {
float ans = length * width;
return (float) ans;
}
public float getPerimeter() {
float ans = 2 * (length + width);
return (float) ans;
}
}
public class Main {
static public void main(String[] args) {
Scanner input = new Scanner(System.in);
int n = input.nextInt();
for (int i = 0; i < n; i++) {
int flag = input.nextInt();
if (flag == 1) {
float radius = input.nextFloat();
Circle c = new Circle(radius);
String ans1 = String.format("%.2f", c.getArea());
String ans2 = String.format("%.2f", c.getPerimeter());
System.out.println(ans1 + " " + ans2);
} else {
float length = input.nextFloat();
float width = input.nextFloat();
Rectangle m = new Rectangle(length, width);
String ans1 = String.format("%.2f", m.getArea());
String ans2 = String.format("%.2f", m.getPerimeter());
System.out.println(ans1 + " " + ans2);
}
}
}
}
**
7-5 jmu-Java-03面向对象基础-02-构造函数与初始化块 (15 分)
**
##1.定义一个Person类 属性:name(String)、gender(boolean)、age(int)、id(int) ,所有的变量必须为私有(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对象,然后直接打印该对象。
##思考 初始化类与对象有几种方法,构造函数、初始化块、静态初始化块。这三种方法执行的先后顺序是什么?各执行几次。
输入样例:
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;
class Person {
private String name;
private int age;
private boolean gender;
private int id = num;
private static int num = 0;
public Person() {
System.out.println("This is constructor");
}
public Person(String name, int age, boolean gender) {
this.name = name;
this.gender = gender;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public boolean getGender() {
return gender;
}
public void setGender(boolean gender) {
this.gender = gender;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
{
num++;
System.out.println("This is initialization block, id is " + id);
}
public String toString() {
return "Person [name=" + name + ", age=" + age + ", gender=" + gender + ", id=" + id + "]";
}
}
public class Main {
static {
System.out.println("This is static initialization block");
}
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
Person p[] = new Person[n];
for (int i = 0; i < p.length; i++) {
String a = in.next();
int b = in.nextInt();
boolean c = in.nextBoolean();
p[i] = new Person(a, b, c);
}
for (int i = n - 1; i >= 0; i--) {
System.out.println(p[i]);
}
Person p1 = new Person();
System.out.println(p1.getName() + "," + p1.getAge() + "," + p1.getGender() + "," + p1.getId());
System.out.println(p1);
}
}
**
7-6 jmu-Java-03面向对象基础-05-覆盖 (15 分)
**
Java每个对象都继承自Object,都有equals、toString等方法。
现在需要定义PersonOverride类并覆盖其toString与equals方法。
###1. 新建PersonOverride类
a. 属性:String name、int age、boolean gender,所有的变量必须为私有(private)。
b. 有参构造函数,参数为name, age, gender
c. 无参构造函数,使用this(name, age,gender)调用有参构造函数。参数值分别为"default",1,true
d.toString()方法返回格式为:name-age-gender
e. equals方法需比较name、age、gender,这三者内容都相同,才返回true.
###2. main方法 2.1 输入n1,使用无参构造函数创建n1个对象,放入数组persons1。
2.2 输入n2,然后指定name age gender。每创建一个对象都使用equals方法比较该对象是否已经在数组中存在,如果不存在,才将该对象放入数组persons2。
2.3 输出persons1数组中的所有对象
2.4 输出persons2数组中的所有对象
2.5 输出persons2中实际包含的对象的数量
2.5 使用System.out.println(Arrays.toString(PersonOverride.class.getConstructors()));输出PersonOverride的所有构造函数。
提示:使用ArrayList代替数组大幅复简化代码,请尝试重构你的代码。
输入样例:
1
3
zhang 10 true
zhang 10 true
zhang 10 false
输出样例:
default-1-true
zhang-10-true
zhang-10-false
2
[public PersonOverride(), public PersonOverride(java.lang.String,int,boolean)]
源代码:
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Scanner;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner se=new Scanner (System.in);
int n1=se.nextInt();
PersonOverride persons1[] =new PersonOverride[n1];
for(int i=0;i<n1;i++) {
persons1[i]=new PersonOverride();
System.out.println(persons1[i].toString());
}
int n2=se.nextInt();
ArrayList<PersonOverride> persons2=new ArrayList<PersonOverride>();
for(int i=0;i<n2;i++) {
PersonOverride a=new PersonOverride(se.next(),se.nextInt(),se.nextBoolean());
if(!persons2.contains(a)) {
persons2.add(a);
System.out.println(a.toString());
}
}
System.out.println(persons2.size());
System.out.println(Arrays.toString(PersonOverride.class.getConstructors()));
}
}
class PersonOverride {
private String name;
private int age;
private boolean gender;
public PersonOverride() {
this.name = "default";
this.age = 1;
this.gender = true;
}
public PersonOverride(String name, int age, boolean gender) {
super();
this.name = name;
this.age = age;
this.gender = gender;
}
public String toString() {
return name + "-" + age + "-" + gender;
}
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + age;
result = prime * result + (gender ? 1231 : 1237);
result = prime * result + ((name == null) ? 0 : name.hashCode());
return result;
}
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
PersonOverride other = (PersonOverride) obj;
if (age != other.age)
return false;
if (gender != other.gender)
return false;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
return true;
}
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;
}
public boolean isGender() {
return gender;
}
public void setGender(boolean gender) {
this.gender = gender;
}
}