1.简易计算器 使用静态方法模拟一个只能进行两个数加,减,乘,除的简易计算器,效果如下图所示。
答案:
public class Calculator {
public static double add(double a, double b) {
return a + b;
}
public static double minus(double a, double b) {
return a - b;
}
public static double multiply(double a, double b) {
return a * b;
}
public static double divide(double a, double b) {
return a / b;
}
public static void main(String[] args) {
System.out.println("4.4加上7.11的结果:" + Calculator.add(4.4, 7.11));
System.out.println("8.9减去2.28的结果:" + Calculator.minus(8.9, 2.28));
System.out.println("5.2乘以13.14的结果:" + Calculator.multiply(5.2, 13.14));
System.out.println("92除以89的结果:" + Calculator.divide(92, 89));
}
}
2.购买电影票 购买电影票有优惠:满18周岁的付40元,未满18周岁的享受半价。使用成员变量,成员方法,构造方法和this关键字,控制台输出如下图所示的姓名,年龄,票价等信息。
答案:
public class Audience {
String name; //姓名
int age; //年龄
public Audience(String name, int age) {
this.name = name;
this.age = age;
}
public void buyTicket() { //购票方法
if(this.age >= 18) { //观众已满18周岁
System.out.println(name + "\t\t" + age + "\t\t" + 40);
} else {//观众未满18周岁
System.out.println(name + "\t\t" + age + "\t\t" + 40/2);
}
}
public static void main(String[] args) {
System.out.println("姓名\t\t年龄\t\t票价(元)");
System.out.println("———————————————————————————————————————");
Audience audience1 = new Audience("李明", 20); //李明,20岁
audience1.buyTicket(); //李明购票
Audience audience2 = new Audience("钱丽", 16); //钱丽, 16岁
audience2.buyTicket(); //钱丽购票
Audience audience3 = new Audience("周刚", 8); //周刚, 8岁
audience3.buyTicket(); //周刚购票
Audience audience4 = new Audience("吴红", 32); //吴红,32岁
audience4.buyTicket(); //吴红购票
}
}
3.计算平均分 使用成员变量,成员方法,构造方法和this关键字,先记录4名学生的语文,数学,英语3科成绩,再计算每个人的平均分。运行结果如下图所示。
答案:
public class Student {
int number; //学号
String name; //姓名
float chinese; //语文成绩
float math; //数学成绩
float english; //英语成绩
public Student(int number, String name, float chinese, float math, float english) { //构造有参方法,并用this关键字区分重名的成员变量与变量
this.number = number;
this.name = name;
this.chinese = chinese;
this.math = math;
this.english = english;
}
public void average() { //计算平均分的方法
float aver = (chinese + math + english)/3; //aver表示平均分
System.out.println(number + "\t" + name + "\t" + chinese + "\t" + math + "\t" + english + "\t" + aver);
}
public static void main(String[] args) {
System.out.println("学号\t姓名\t语文\t数学\t英语\t平均分");
System.out.println("——————————————————————————————————————————————————");
Student student1 = new Student(1, "张三", 91.5f, 98f, 89f); //张三的各科成绩
student1.average(); //计算张三的平均分
Student student2 = new Student(2, "李四", 96f, 98.5f, 93f); //李四的各科成绩
student2.average(); //计算李四的平均分
Student student3 = new Student(3, "王五", 97f, 100f, 98.5f); //王五的各科成绩
student3.average(); //计算王五的平均分
Student student4 = new Student(4, "钱六", 77f, 83f, 81f); //钱六的各科成绩
student4.average(); //计算钱六的平均分
}
}
4.厘米与英寸互转 编写工具类,提供厘米与英寸之间的相互转换的工具方法。
答案:
public class Length {
static double ratio = 2.54; // 厘米与英寸之间的转换系数
public static double transferCM(double cm) { // “厘米转换为英寸”的方法
double inch = cm / ratio;
return inch;
}
public static double transferINCH(double inch) { // “英寸转换为厘米”的方法
double cm = inch * ratio;
return cm;
}
public static void main(String[] args) {
System.out.println("10厘米转成英寸的结果为:" + transferCM(10));
System.out.println("10英寸米转成厘米的结果为:" + transferINCH(10));
}
}
5.多种权限的工具类 创建一个类,在该类中,getRandomNumber()方法可以被所有人使用,setNUmber()方法只可以被同包下的类使用,sort()方法只能自己使用。
答案:
public class Demo {
public void getRandomNumber() {
}
void setNumber() {
}
private void sort() {
}
}
6.计算矩形面积 尝试编写一个矩形类,将长和宽作为矩形类的属性,在构造方法中将长,宽初始化,定义一个成员方法求此矩形的面积。
答案:
public class Rect {
private float height;
private float width;
public Rect(float height, float width) {
this.height = height;
this.width = width;
}
public float square() {
return height * width;
}
public static void main(String args[]) {
Rect u = new Rect(3.5f, 4.5f);
System.out.println(u.square());
}
}
7.判断是否存在运行时参数 编写一个类,将mian方法的所有运行参数输出到控制台中,如果没有运行时参数,则输出“无运行参数”提示。
public class ArgsDemo {
public static void main(String[] args) {
int lengh = args.length;
if (lengh == 0) {
System.out.println("无运行参数");
} else {
for (int i = 1; i <= args.length; i++) {
System.out.println(i);
}
}
}
}
8.单例模式 创建一个类,该类无法通过构造方法创建对象,只能通过该类提供的getInstance()静态方法获得该类对象。
答案:
public class Demo {
private Demo() {
}
public static Demo getInstance() {
return new Demo();
}
}