在这一讲中我会给出一些关于面向对象部分的习题,同样希望在不看答案的情况下自己编写,即使看过了答案,也要能够在不看答案的情况下写出来。
类的定义
员工类Employee
定义在同一个公司工作的Employee类,要求其中含有属性:员工的名字,员工的年龄,员工的爱好,员工的公司名(注意当公司更名时,所有员工的公司名都需要更名),工作地点默认为中国(同公司名),初始化员工名字为大明,年龄为0岁
答案:
class Students{
String name;
int age;
String hobbies;
static String company;
static String place = "China";
Students(){
this.name = "大明";
this.age = 0;
}
}
求和类Sum
定义一个求和类Sum类,该类不含任何属性变量,且仅有构造方法,要求创建对象时输入int类型的两参或三参时能输出参数之和
答案:
class Sum{
Sum(int a, int b){
System.out.println(a + b);
}
Sum(int a, int b, int c){
System.out.println(a + b + c);
}
}
类与对象
书籍类Book
创建一个书籍类Book类,其中包含书籍的名字和书籍的页码数,其中包含书名和页码数的get和set方法,和detail方法用于打印书籍的信息。(要求:输入页码数如果少于200,则提示并默认设置为200)
答案:
class Book{
private String title;
private int pageNum;
Book(){}
void setTitle(String title){
this.title = title;
}
String getTitle(){
return this.title;
}
void setPageNum(int pageNum){
if (pageNum < 200) {
pageNum = 200;
System.out.println("Incorrect input!");
}
this.pageNum = pageNum;
}
int getPageNum(){
return this.pageNum;
}
void detail(){
System.out.println("Title: " + this.title);
System.out.println("Page number: " + this.pageNum);
}
}
Book类的测试类BookTest
创建用于测试之间的Book类的测试类BookTest类,要求实例化时就直接完成测试,能够调用Book类中的set方法和detail方法
答案:
class BookTest{
BookTest(String title, int pageNum){
Book book = new Book();
book.setTitle(title);
book.setPageNum(pageNum);
book.detail();
}
}
异常
创建一个int类型变量age,并通过输入赋值,使用try-catch语句捕获可能出现的异常,并且自定义一个不受检异常类AgeException类,当输入年龄大于200或者小于0时主动抛出该异常
答案:
import java.util.InputMismatchException;
import java.util.Scanner;
public class Exception {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int age = 0;
try {
age = scanner.nextInt();
if (age < 0 || age > 200)
throw new AgeException("");
}
catch (InputMismatchException e){
System.out.println("Please input an integer");
}
catch (AgeException e){
System.out.println("Unacceptable age");
}
}
}
class AgeException extends RuntimeException{
AgeException(String str){
System.out.println(str);
}
}
能扩容的MyList类
定义一个MyList类,仅有一个属性Object[] element,其中需要有以下方法
- add方法,可以向数组属性中依次存储Object,数组内容 存满时,需实现动态扩容。
- remove方法,可以根据数据或下标,从数组属性中删除Object数据,删除后,数组后续元素需前移。
- get方法,传入下标,返回数组中指定下标的数据。
- size方法,获取当前存储的有效数据长度
提示:无需真正增加原数组的容量,只用将原内容复制到新 的大数组,然后让原数组名称重新等于大数组即可。由于原数组数据在堆中, 失去引用会被GC自动回收。
class MyList{
private Object[] element = new Object[1];
private int currentPosition = 0;
void add(Object obj){
if(currentPosition + 1 > this.element.length){
Object[] element = new Object[this.element.length + 1];
for(int i = 0; i < currentPosition; i++)
element[i] = this.element[i];
this.element = element;
}
this.element[currentPosition] = obj;
currentPosition++;
}
void remove(int position){
if(position < currentPosition && position >= 0) {
for (; position < currentPosition - 1; position++) {
element[position] = element[position + 1];
}
currentPosition--;
}
else
System.out.println("Position not exist");
}
void removeByElement(Object obj){
int position = -1;
for(int i = 0; i < currentPosition; i++){
if(obj == element[i]) {
position = i;
this.remove(position);
return;
}
}
System.out.println("Element not exist");
}
Object get(int position){
if(position > currentPosition || position < 0) {
System.out.println("Position not exist, return the zeroth element");
position = 0;
}
return element[position];
}
int getSize(){
return element.length;
}
}
剪刀石头布
定义一个AI类实现人机对战的猜拳游戏,用户通过输入(1.剪刀2.石头3.布),机器随机生成,胜利则加一分,失败扣一分, 10局以后打印积分的值
要求使用面向对象的思想完成该石头剪刀布的游戏(在main方法中不含有任何输出和运算),在AI类中至少定义score方法(用户输入和积分加减)和judge方法(判断胜负)
答案:
import java.util.Scanner;
public class PaperScissorStone {
public static void main(String[] args) {
AI ai = new AI();
}
}
class AI {
int aiDecision;
AI() {
this.score();
}
void random() {
this.aiDecision = (int) (Math.random() * 3 + 1);
}
void score(){
Scanner scanner = new Scanner(System.in);
int userInput;
int score = 0;
for (int i = 0; i < 10; i++) {
System.out.print("Input your decision, 1 for scissor, 2 for stone, and 3 for paper:");
userInput = scanner.nextInt();
score += this.judge(userInput);
System.out.println("Now your score is: " + score);
}
}
int judge(int userInput) {
this.random();
if (aiDecision == userInput) {
System.out.println("Tie");
return 0;
} else if (userInput == 1) {
if (aiDecision == 2) {
System.out.println("You lose");
return -1;
} else if (aiDecision == 3) {
System.out.println("You win");
return 1;
}
} else if (userInput == 2) {
if (aiDecision == 3) {
System.out.println("You lose");
return -1;
} else if (aiDecision == 1) {
System.out.println("You win");
return 1;
}
} else if (userInput == 3) {
if (aiDecision == 2) {
System.out.println("You lose");
return -1;
} else if (aiDecision == 1) {
System.out.println("You win");
return 1;
}
}
return -10;// 表明出现错误了
}
}