7-1 sdut-oop-4-求圆的面积(类与对象)
按要求完成程序的编写。
1、定义圆类Circle,其中包括:
(1)成员变量:private int radius
(2)无参构造方法 ,给radius赋值为2,并输出信息:"This is a constructor with no para.";
(2)有参构造方法 ,接收用户给的radius赋值,并输出"This is a constructor with para."(如果给的半径小于等于0,则赋默认值为2);
(3)为radius添加set方法,接收用户输入的半径,如果用户输入半径为<=0,则让半径的值为2;
(4)为radius半径添加get方法,返回用户输入的半径;
(5)求圆面积方法getArea(), π使用Math.PI代替;
(6)定义toString方法,public String toString( )方法体为:
return "Circle [radius=" + radius + "]";
2、定义Main类,在main方法中,完成下列操作:
(1)定义并创建Circle类对象c1,输出c1对象,求c1的面积并输出;
(2)定义并创建Circle类对象c2,输出c2对象,求c2的面积并输出;
(3)从键盘接收整数半径,赋值给c2的半径,输出c2对象,求c2的面积并输出;
(4)从键盘接收整数半径,用有参构造方法创建Circle类对象c3,输出c3对象,求c3的面积后输出。
输入格式:
输入有多行。每行包括一个整数的圆的半径。
输出格式:
按照题目要求,输出圆构造方法的输出信息、圆对象的字符中描述信息、及圆的面积(其中圆的面积保留2位小数)。上述信息各占一行。
输入样例:
4
5
输出样例:
This is a constructor with no para.
Circle [radius=2]
12.57
This is a constructor with no para.
Circle [radius=2]
12.57
Circle [radius=4]
50.27
This is a constructor with para.
Circle [radius=5]
78.54
代码:
import java.util.Scanner;
class Circle {
private int radius;
double volume;
Circle() {
System.out.println("This is a constructor with no para.");
radius = 2;
}
Circle(int i) {
System.out.println("This is a constructor with para.");
if (i <= 0)
radius = 2;
else
radius = i;
}
void setRadius(int i) {
if (i <= 0)
radius = 2;
else
radius = i;
}
int getRadius(){
return radius;
}
double getArea(){
volume = Math.PI*radius*radius;
return volume;
}
public String toString() {
return "Circle [" + "radius=" + radius + ']';
}
}
public class Main {
public static void main(String[] args) {
Circle c1 = new Circle();
System.out.println(c1.toString());
System.out.printf("%.2f\n",c1.getArea());
Circle c2 = new Circle();
System.out.println(c2.toString());
System.out.printf("%.2f\n",c2.getArea());
Scanner sc = new Scanner(System.in);
c2.setRadius(sc.nextInt());
System.out.println(c2.toString());
System.out.printf("%.2f\n",c2.getArea());
Circle c3 = new Circle(sc.nextInt());
System.out.println(c3.toString());
System.out.printf("%.2f",c3.getArea());
}
}
7-2 学生类-构造函数
定义一个有关学生的Student类,内含类成员变量:
String name、String sex、int age,所有的变量必须为私有(private)。
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.*;
public class Main {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
String name = scan.next();
int age = scan.nextInt();
String sex = scan.next();
Student s1 = new Student(name,age,sex);
System.out.print(s1);
}
}
class Student {
private String name;
private String sex;
private int age;
public Student(String name,int age,String sex) {
super();
this.name = name;
this.age = age;
this.sex = sex;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String toString() {
return String.format("Student [name='%s', sex='%s', age=%d]",this.name,this.sex,this.age);
}
}
7-3 设计一个矩形类Rectangle
编写一个Java应用程序,该程序包含两个类,类的定义如下:
(1) 一个有关计算矩形面积的类Rectangle
,定义如下成员:
① 两个私有的成员变量:length
(长,double类型)、width
(宽,double类型);
② 一个公有的无参数的构造方法,该构造方法将所有成员变量初始化为零;
③ 一个公有的有参数的方法void setXY(double a, double b)
,该方法用于设置矩形的属性length与width;
④ 一个公有的无参数的方法double getArea( )
,该方法计算并返回矩形的面积;
⑤一个公有的无参数的方法double getPerimeter( )
,该方法计算并返回矩形的周长;
(2) 一个测试类Main
,在main方法中声明1个Rectangle类的对象rect
,通过setXY方法给rect的属性length和width进行赋值(从键盘输入),通过getArea方法来计算rect的面积,通过getPerimeter方法来计算rect的周长,然后输出其面积与周长。
输入格式:
输入两个数,作为矩形的长、宽。
输出格式:
第一行输出矩形的面积。
第二行输出矩形的周长。
输入样例:
3.5 2
输出样例:
面积为7.0
周长为11.0
代码:
import java.util.*;
class Rectangle{
private double width = 1;
private double heigth = 1;
public Rectangle() {
}
public Rectangle(double width, double heigth) {
this.width = width;
this.heigth = heigth;
}
public double getArea(){
return this.width * this.heigth;
}
public double getPerimeter(){
return ( this.heigth + this.width) * 2;
}
}
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
double wide = input.nextDouble();
double hight = input.nextDouble();
Rectangle myRectangle = new Rectangle(wide, hight);
System.out.println("面积为"+myRectangle.getArea());
System.out.print("周长为"+myRectangle.getPerimeter());
input.close();
}
}