# Java程序设计__Chap3 面对对象__编程题

## 6-1 设计一个矩形类Rectangle (10分)

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
{
private double width;
private double height;
public Rectangle(){}
public Rectangle(double w,double h)
{
width=w;
height=h;
}
double getArea()
{
return width*height;
}
double getPerimeter()
{
return (width+height)*2;
}
}


## 7-1 类的定义与对象使用 (10分)

3
20174042001 zhangsan 20
20174042030 lisi 2
20174042050 wangwu 17


zhangsan 20174042001 20
wangwu 20174042050 17


import java.util.*;
class Student
{
private String sno;
private String name;
private int age;
public Student(String s1,String n,int a)
{
sno=s1;
name=n;
age=a;
}
public String toString()
{
return name+" "+sno+" "+age;
}

}
public class Main
{
public static void main(String[] args)
{
int k;
Scanner s=new Scanner(System.in);
k=s.nextInt();
Student stu[]=new Student[k];
for(int i=0;i<k;i++)
{
String sno1=s.next();
String name1=s.next();
int age1=s.nextInt();
else
{
stu[i]=new Student(sno1,name1,age1);
System.out.println(stu[i]);
}
}
}
}


## 7-2 好玩的图形 (20分)

interface Shape{
float getArea();//求面积
float getPerimeter();//求周长
}


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 Round implements Shape
{

public Round(float r)
{
}

public float getArea()
{
}
public float getPerimeter()
{
}
}
class Rect implements Shape
{
private float length;
private float width;
public Rect(float l,float w)
{
length=l;width=w;
}
public float getArea()
{
return (float)(length*width);
}
public float getPerimeter()
{
return (float)(2*(length+width));
}
}
public class Main
{
public static void main(String[] args)
{
Scanner ss=new Scanner(System.in);
int n=ss.nextInt();
for(int i=1;i<=n;i++)
{
int m=ss.nextInt();
if(m==1)
{
float r=ss.nextFloat();
Round r1=new Round(r);
System.out.println(String.format("%.2f",r1.getArea())+" "+String.format("%.2f",r1.getPerimeter()));
}
else if(m==2)
{
float l=ss.nextFloat();
float w=ss.nextFloat();
Rect r2=new Rect(l,w);
System.out.println(String.format("%.2f",r2.getArea())+" "+String.format("%.2f",r2.getPerimeter()));
}
}
}
}


## 7-3 声明图书类，记录图书总册数，利用静态变量赋值。 (10分)

书名：Java程序设计, 书号：1, 书价：34.5



class Book
{
private String title;
private double price;
public static String sno;
public static int num=0;
public Book(String t,double p)
{
num++;
title=t;
price=p;
sno=Integer.toString(Integer.parseInt("0")+num);
}
public String toString()
{
return "书名："+title+", 书号："+sno+", 书价："+price;
}
public static int getNum()
{
return num;
}
}
public class Main
{
public static void main(String[] args){
Book a=new Book("Java程序设计",34.5);
System.out.println(a);
Book b=new Book("数据结构",44.8);
System.out.println(b);
Book c=new Book("C++程序设计",35.0);
System.out.println(c);
System.out.println("图书总册数为："+Book.getNum());
}
}


## 7-4 jmu-Java-03面向对象基础-04-形状-继承 (15分)

1、定义抽象类Shape 属性：不可变静态常量double PI，值为3.14， 抽象方法:public double
getPerimeter(),public double getArea() 2、Rectangle与Circle类均继承自Shape类。

toString方法(Eclipse自动生成) 3、编写double sumAllArea方法计算并返回传入的形状数组中所有对象的面积和与
double sumAllPerimeter方法计算并返回传入的形状数组中所有对象的周长和。 4、main方法
4.1 输入整型值n，然后建立n个不同的形状。如果输入rect，则依次输入宽、长。如果输入cir，则输入半径。
4.2 然后输出所有的形状的周长之和，面积之和。并将所有的形状信息以样例的格式输出。 提示：使用Arrays.toString。
4.3 最后输出每个形状的类型与父类型.使用类似shape.getClass() //获得类型, shape.getClass().getSuperclass() //获得父类型;

4
rect
3 1
rect
1 5
cir
1
cir
2


38.84
23.700000000000003
class Rectangle,class Shape
class Rectangle,class Shape
class Circle,class Shape
class Circle,class Shape


import java.util.*;
abstract class Shape
{
static final double  PI=3.14;
public abstract double getPerimeter();
public abstract double getArea();
}
class Rectangle extends Shape
{
private int width;
private int length;
public Rectangle(int width,int length)
{
this.width=width;
this.length=length;
}
public double getPerimeter()
{
return 2*(width+length);
}
public double getArea()
{
return length*width;
}
public String toString()
{
return "Rectangle [width="+width+", length="+length+"]";
}
}
class Circle extends Shape
{
{
}
public double getPerimeter()
{
}
public double getArea()
{
}
public String toString()
{
}
}
public class Main
{
public static void main(String[] args)
{
double sumAllArea=0,sumAllPerimeter=0;
Scanner ss=new Scanner(System.in);
int n=ss.nextInt();
Shape sha[]=new Shape[n];
for(int i=0;i<n;i++)
{
String s1=ss.next();
if(s1.equals("rect"))
{
int w=ss.nextInt();
int l=ss.nextInt();
sha[i]=new Rectangle(w,l);
sumAllArea+=sha[i].getArea();
sumAllPerimeter+=sha[i].getPerimeter();
}
else if(s1.equals("cir"))
{
int r=ss.nextInt();
sha[i]=new Circle(r);
sumAllArea+=sha[i].getArea();
sumAllPerimeter+=sha[i].getPerimeter();
}
}
System.out.println(sumAllPerimeter);
System.out.println(sumAllArea);
System.out.print("[");
for(int i=0;i<n;i++)
{
if(i!=0) System.out.print(", ");
System.out.print(sha[i]);
}
System.out.println("]");
for(int i=0;i<n;i++)
{
if(sha[i] instanceof Rectangle) System.out.println("class Rectangle,class Shape");
else if(sha[i] instanceof Circle) System.out.println("class Circle,class Shape");
}
}
}


## 7-5 学生类-构造函数 (10分)

1.编写有参构造函数： 能对name,sex,age赋值。

2.覆盖toString函数： 按照格式：类名 [name=, sex=, age=]输出。使用idea自动生成,然后在修改成该输出格式

3.对每个属性生成setter/getter方法
4.main方法中 •输入1行name age sex , 调用上面的有参构造函数新建对象。

tom 15 male


Student [name='tom', sex='male', age=15]


import java.util.*;
class Student
{
private String name;
private String sex;
private int age;
public Student(String n,String s,int a)
{
name=n;
sex=s;
age=a;
}
public String toString()
{
return "Student [name='"+name+"', sex='"+sex+"', age="+age+"]";
}
}
public class Main
{
public static void main(String[] args)
{
Scanner ss=new Scanner(System.in);
String name=ss.next();
int age=ss.nextInt();
String sex=ss.next();
Student s=new Student(name,sex,age);
System.out.println(s);
}
}


## 7-6 计算年龄 (10分)

1995
12
23


age=22


import java.util.*;
class Birthday
{
private int year;
private int month;
private int day;
public Birthday(int y,int m,int d)
{
year=y;month=m;day=d;
}
public void setYear(int y1)
{
year=y1;
}
public void setMonth(int m1)
{
month=m1;
}
public void setDay(int d1)
{
day=d1;
}
public int getYear()
{
return year;
}
public int getMonth()
{
return month;
}
public int getday()
{
return day;
}
public int getAge()
{
if(day<=25)return 2017-year;
else return 2016-year;
}
}

public class Main
{
public static void main(String[] args)
{
Scanner ss=new Scanner(System.in);
int y=ss.nextInt();
int m=ss.nextInt();
int d=ss.nextInt();
Birthday b=new Birthday(y,m,d);
System.out.println("age="+b.getAge());
}
}

• 点赞
• 评论
• 分享
x

海报分享

扫一扫，分享海报

• 收藏
• 手机看

分享到微信朋友圈

x

扫一扫，手机阅读

• 打赏

打赏

呐_kululu

你的鼓励将是我创作的最大动力

C币 余额
2C币 4C币 6C币 10C币 20C币 50C币
• 一键三连

点赞Mark关注该博主, 随时了解TA的最新博文

05-04 1887
10-16 487
05-13 1012
07-11 3055
04-20 129