选择题
1. Retangle类
【问题描述】
定义一个 Retangle类,有长、宽等属性,定义构造方法用来初始化类的这些属性,定义方法输出Retangle的长宽和面积。编写应用程序使用Retangle。
【输入样例】
Please input length: 4.0
Please input width: 3.0
【输出样例】
The lengh is:4.0
The width is:3.0
The area is:12.0
【代码示例】
import java.util.Scanner;
public class Ret {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.println("Please input length:");
double x= in.nextDouble();
System.out.println("Please input width:");
double y = in.nextDouble();
retangle r = new retangle(x,y); // 用x,y对长方形的长和宽进行初始化(初始化和赋值不一样,构造方法不能赋值)
System.out.println("The lengh is:"+r.length);
System.out.println("The width is:"+r.width);
System.out.println("The area is:"+r.area());
}
}
class retangle{ //定义retangle类,具有长和宽的属性
double length;
double width;
public retangle(double x , double y){ //构造时候就需要对长和宽初始值,选择直接使用参数构造
length = x;
width = y;
}
double area(){
return length*width;
} //输出面积的方法
}
2.圆类
【问题描述】定义一个描述圆的类,名字为Circle,属性有:
private double radius
方法有:
double getPerimeter(),计算圆的周长
double getArea(),计算圆的面积
void disp(),输出圆的半径、周长和面积
另外,有两个构造方法:
Circle(),属性半径赋值为0
Circle(double r),属性半径赋值为r
编写main方法,从键盘输入一个小数值,作为圆的半径,并生成该对象,最后调用disp()方法,输出结果。
【输入形式】小数,圆的半径值。
【输出形式】圆的半径、直径和面积,小数位保留4位。
【样例输入1】
3
【样例输出1】
radius=3.0000
perimeter=18.8496
area=28.2743
【样例输入2】
5
【样例输出2】
radius=5.0000
perimeter=31.4159
area=78.5398
【代码示例】
import java.util.Scanner;
public class Circle {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
double r = in.nextDouble();
Cir c = new Cir(r); //定义一个圆c,初始化半径为r
c.disp();
}
}
class Cir{ // 一个圆类,只有属性半径radius
private double radius;
//对于圆有两个构造方法,无参构造默认半径为0,若有参构造,则初始化为r
public Cir(double r) { // 构造方法对圆类的属性radiius进行初始化
radius = r;
}
public Cir() { //无参构造方法对圆类的属性radiius进行初始化0
radius = 0;
}
double getPerimeter(){ // 普通方法,返回圆周长
return 2*Math.PI*radius;
}
double getArea(){ //普通方法,返回圆面积
return Math.PI*radius*radius;
}
void disp(){ // disp()方法,可以输出圆的半径;并在其中调用getPerimeter()和getArea()方法,输出周长和面积
System.out.printf("radius=%.4f\n",radius);
System.out.printf("perimeter=%.4f\n",getPerimeter());
System.out.printf("area=%.4f\n",getArea());
}
}
3.Employee类
【问题描述】
定义一个 Employee类,有名字、年龄、基本收入、分红收入等属性,定义构造方法用来初始化类的这些属性。
定义方法输出Employee的名字,年龄,总收入(基本收入和分红收入的和)。编写应用程序使用Employee。
【输入形式】
请输入员工的名字:
请输入员工的年龄:
请输入员工的基本收入:
请输入员工的分红收入:
【输出形式】
员工的名字:
员工的年龄:
员工的总收入(基本收入+分红收入):
【输入样例】
Please input the employee's name: Tom
Please input the employee's age: 20
Please input the employee's sarary: 3000.0
Please input the employee's bonus: 4000.0
【输出样例】
Employee's name: Tom
Employee's age: 20
Employee's income: 7000.0
【代码示例】
import java.util.Scanner;
public class Employ {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
Employee employee = new Employee();
System.out.println("Please input the employee's name: ");
String name = in.nextLine();
employee.setName(name); //可以使用方法对其修改,也可直接修改
System.out.println("Please input the employee's age: ");
int age = in.nextInt();
employee.age = age; // 因为employee中的age不是private。所以可以直接访问修改,也可以调用setAge()方法修改
System.out.println("Please input the employee's sarary:");
double income = in.nextDouble();
employee.setIncome(income);
System.out.println("Please input the employee's bonus:");
double income_more = in.nextDouble();
employee.income_more = income_more;
System.out.println("Employee's name: "+employee.name);
System.out.println("Employee's age: "+age);
System.out.println("Employee's income: "+employee.allIncome());
}
}
class Employee{
String name = "0";
int age = 0;
double income = 0;
double income_more = 0;
void setName(String s){
name = s;
}
void setAge(int a){
age = a;
}
void setIncome(double in){
income = in;
}
void setIncome_more(double in){
income_more = in;
}
double allIncome(){
return income_more+income;
}
}
4.MyPoint类
【问题描述】
编写一个MyPoint类,该类拥有横、纵坐标两个属性,定义构造方法来初始化类的这些属性,定义方法输出MyPoint的信息。编写应用程序使用MyPoint。
【输入形式】
请输入横坐标(X):
请输入纵坐标(Y):
【输出形式】
你的位置的横坐标(X)为:
你的位置的纵坐标(Y)为:
【输入样例】
Please input the horizontal (X) : 30.5
Please input the ordinate (Y) : 75.6
【输出样例】
Your location of horizontal (X) is : 30.5
Your location of ordinate (Y) is : 75.6
【代码示例】
import java.util.Scanner;
public class Point {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.println("Please input the horizontal (X) :");
double x = in.nextDouble();
System.out.println("Please input the ordinate (Y) :");
double y = in.nextDouble();
MyPiont myPiont = new MyPiont(x,y);
myPiont.get();
}
}
class MyPiont{
double x;
double y;
public MyPiont(){ //无参构造方法
x=0;
y=0;
}
public MyPiont(double x, double y){
this.x=x;
this.y=y;
}
void get(){
System.out.println("Your location of horizontal (X) is :"+x);
System.out.printf("Your location of ordinate (Y) is :"+y);
}
}