JAVA学习-04-韩顺平老师
JAVA-类与对象简单练习
1.编写类Book,定义方法updatePrice,实现更改书的价格
具体:如果价格>150 就更改为150,如果价格>100,更该为100
否则不变。
public class Test {
public static void main(String[] args) {
Book book = new Book();
book.updatePrice(30);
System.out.println("book当前的价格是:" + book.getPrice());
}
}
class Book { // 创建一个类
int price;
public void updatePrice(int price) {
if(price <= 100) {
this.price = price;
} else if(price > 100 && price <= 150) {
this.price = 100;
} else if(price > 150) {
this.price = 150;
} else {
System.out.println("请输入一个正确的值,现在给你个默认值 price = 10");
this.price = 10;
}
}
public int getPrice(){
return this.price;
}
}
2.定义一个圆类,提供显示元周长的功能的方法,提供显示圆面积的方法
public class Test {
public static void main(String[] args) {
Circle circle = new Circle(3);
circle.area();
circle.perimeter();
}
}
class Circle { // 创建一个类
int radius;
public Circle(int radius) {
this.radius = radius;
}
public void area() {
System.out.println("当前圆的面积为:" + this.radius * this.radius * Math.PI);
}
public void perimeter(){
System.out.println("当前圆的周长为:" + 2 * this.radius * Math.PI);
}
}
3.编程创建一个Cale类,在其中定义两个变量操作数,定义四个方法分别实现求 和、差、乘积、商,并进行测试。
public class Test {
public static void main(String[] args) {
Cale cal = new Cale(20.0,10.0);
System.out.println("和=" + cal.add() + " 差=" + cal.sub() + " 乘积=" + cal.ride() + " 商=" + cal.except());
}
}
class Cale { // 创建一个类
double num1;
double num2;
public Cale(double num1, double num2) {
this.num2 = num2;
this.num1 = num1;
}
public double add() {
return this.num1 + this.num2;
}
public double sub(){
return this.num1 - this.num2;
}
public double ride(){
return this.num1 * this.num2;
}
public double except(){
return this.num1 / this.num2;
}
}
4.创建一个Employee类,属性有(名字,性别,年龄,职位,薪水)。
提供三个构造方法,可以初始(1)(名字,性别,年龄,职位,薪水,)
(2)(名字,性别,年龄)
(3)(职位,薪水) 充分使用复用构造器
public class Test {
public static void main(String[] args) {
Employee employee1 = new Employee("jack", '男', 28, "教师", 3000);
Employee employee2 = new Employee("perter", '男', 39);
Employee employee3 = new Employee("医生", 7000);
// 编译通过
}
}
class Employee { // 创建一个类
String name;
char gender;
int age;
String position;
int salary;
// 构造方法一
public Employee(String name, char gender, int age, String position, int salary) {
this.name = name;
this.gender = gender;
this.age = age;
this.position = position;
this.salary = salary;
}
// 重载
public Employee(String name, char gender, int age) {
this.name = name;
this.gender = gender;
this.age = age;
}
public Employee(String position, int salary) {
this.position = position;
this.salary = salary;
}
}
5.在printAreas方法中打印输出1到times之间,每个证书的半径值,以及对应的面积
public class Test {
public static void main(String[] args) {
Circle circle = new Circle();
for (int i = 1; i < 6; i++) {
circle.setRadius(i); // 设置半径值
circle.printAreas(); // 输出对应面积
}
}
}
class Circle { // 创建一个类
int radius;
public void printAreas() {
System.out.println(this.radius + "\t" + this.radius * this.radius * Math.PI);
}
public void setRadius(int radius ){
this.radius = radius;
}
}
6.电脑猜拳游戏
电脑每次随机生成0,1,2
0表示石头,1表示剪刀,2表示布
并要可以显示Tom的输赢次数(清单)
import java.util.Scanner;
import java.util.Random;
public class Test {
public static void main(String[] args) {
int times = 3; // 想玩的次数
Tom tom = new Tom(); // 声明对象
Scanner myScanner = new Scanner(System.in);
String[][] arr = new String[times][3]; // 数组作为清单记录对局详情
for (int i = 0; i < times; i++) { // 循环
tom.setComNum(); // 设置计算机的猜想
arr[i][0] = tom.changeChinese(tom.comGuessNum); // 将猜想放入清单
System.out.print("请输入你的猜拳(0表示石头,1表示剪刀,2表示布):"); // 提示输入
tom.setTomNum(myScanner.nextInt()); // tom输入猜想
arr[i][1] = tom.changeChinese(tom.tomGuessNum); // 与上面的一致
arr[i][2] = tom.compare();
//每次猜拳输出之前已经猜过的结果
System.out.println("============结果如下=================");
System.out.println("电脑\tTom\t输赢");
for (int k = 0; k <= i; k++) {
for(int j = 0; j < arr[k].length; j++){
System.out.print(arr[k][j] + "\t");
}
System.out.println(); // 换行
}
}
System.out.println("游戏结束了~~");
System.out.println("============最终结果如下=================");
System.out.println("电脑\tTom\t输赢");
for (int i = 0; i < arr.length; i++) {
for(int j = 0; j < arr[i].length; j++){
System.out.print(arr[i][j] + "\t");
}
System.out.println();
}
}
}
class Tom { // 创建一个类
int comGuessNum; // 计算机出的数
int tomGuessNum; // tom出的数
// 获得电脑的猜想
public int getComNum() {
Random r = new Random();
comGuessNum = r.nextInt(3); // 随机生成一个0-2的数字,作为计算机出的
return comGuessNum;
}
// 设置电脑的猜想
public void setComNum() {
this.comGuessNum = this.getComNum();
}
// 设置tom的猜想,并对数进行处理
public void setTomNum(int tomNum) {
if(tomNum >= 0 && tomNum <= 2) {
this.tomGuessNum = tomNum;
} else {
this.tomGuessNum = 0; // 如果输入的值有错,则默认给他一个0 出拳头
}
}
// 进行猜拳比较
public String compare(){
if(tomGuessNum == 0 && comGuessNum == 1) {
return "你赢了";
}else if(tomGuessNum == 1 && comGuessNum == 2) {
return "你赢了";
}else if(tomGuessNum == 2 && comGuessNum == 0) {
return "你赢了";
} else if (tomGuessNum == comGuessNum) {
return "平局";
} else {
return "你输了";
}
}
// 将数字转换为中文
public String changeChinese(int num){
if(num == 0) {
return "石头";
} else if (num == 1) {
return "剪刀";
} else {
return "布";
}
}
}