记录
Java练习题
第三章练习题
共四个练习
MobilePhone.java
package practice1;
public class MobilePhone {
private final String name; //只能在三个地方初始化赋值 成员变量被final修饰后没有默认值
/*{
this.name = "HuaWei";
}这个是在程序块赋值*/
//这里用构造方法赋值
public MobilePhone(String name) {
this.name = name;
}
public final void introduce() {
final String color; //局部变量没有默认值
System.out.println("The mobile phone's name is:"+name);
color = "black";
System.out.println("The mobile phone's color is:"+color);
}
}
HuaweiPhone.java
package practice1;
public class HuaweiPhone extends MobilePhone {
public HuaweiPhone(String name) {
//子类中会默认调用父类不带参数的构造方法,若父类中没有不带参数的构造方法,则需要显式调用,即此代码
super(name); //调用父类中带参数的构造方法
}/**/
/*public void introduce() { //子类不能重写父类中被final修饰过的方法
}*/
}
Test1.java
package practice1;
public class Test1 {
//测试类
public static void main(String[] args) {
MobilePhone mp1 = new MobilePhone("XiaoMi");
mp1.introduce();
HuaweiPhone hp1 = new HuaweiPhone("Huawei");
hp1.introduce();
MobilePhone mp2 = new HuaweiPhone("Iphone"); //这就是多态,具体还不知道有什么用法
mp2.introduce();
}
}
TV.java
package practice2_1;
public class TV {
int channel = 0;
int volume = 0;
int tvswitch = 0;
/*1.定义一个电视机类,实现电视机的基本功能(换台,调整音量,开关),并测试。*/
//1. Define a TV set, realize the basic functions of the TV set (channel change,
//volume adjustment, switch), and test it.
public void ChannelChange(int channel) {
this.channel = channel;
System.out.println("现在是"+channel+"台");
}
public void VolumeAdjustment(int volumechange) {
if(volumechange == 1) {
if(volume >= 3) {
System.out.println("已经是最大音量!");
return ;
}
this.volume++;
}
else if(volumechange == 0) {
if(volume <= 0) {
System.out.println("已经是最低音量!");
return ;
}
this.volume--;
}
else
System.out.println("Error!");
System.out.println("现在音量是"+this.volume);
}
public void Switch(int tvswitch) {
if(tvswitch == 1) {
System.out.println("电视打开!");
}
else if(tvswitch == 0) {
System.out.println("电视关闭!");
}
else
System.out.println("Error!");
this.tvswitch = tvswitch;
}
}
Test2_1.java
package practice2_1;
import java.util.Scanner;
public class Test2_1 {
public static void main(String[] args) {
TV tv = new TV();
System.out.println("现在有一台关闭的电视!");
int select = 1;
@SuppressWarnings("resource") //我也不知道是啥。。。不加这个的话inTo会有一个小警告
Scanner inTo = new Scanner(System.in);
System.out.println("1.打开电视!");
System.out.println("0.离开!");
System.out.println("请选择:");
select = inTo.nextInt();
tv.Switch(select);
if(select == 0) {
System.out.println("已退出!");
return;
}
else {
while(select != 0) {
System.out.println("1.换台!");
System.out.println("2.调音量!");
System.out.println("0.关电视!");
System.out.println("请选择:");
select = inTo.nextInt();
switch(select) {
case 1:
int channel = 0;
System.out.println("请输入台数:");
channel = inTo.nextInt();
tv.ChannelChange(channel);
break;
case 2:
int volume = 0;
System.out.println("1.增大音量!");
System.out.println("0.减小音量!");
volume = inTo.nextInt();
tv.VolumeAdjustment(volume);
break;
case 0:
tv.Switch(select);
System.out.println("已退出!");
break;
}
}
}
}
}
后来才发现课本上有例题。。。
下面是课本例题代码
TV.java
package practice2_1_again;
public class TV {
int channel; //电视频道
void setChanner(int m) {
if(m>1) {
channel = m;
}
}
int getChannel() {
return channel;
}
void showProgram() {
switch(channel) {
case 1:
System.out.println("综合频道");
break;
case 2:
System.out.println("经济频道");
break;
case 3:
System.out.println("文艺频道");
break;
case 4:
System.out.println("国际频道");
break;
case 5:
System.out.println("体育频道");
break;
default:
System.out.println("不能收看"+channel+"频道");
}
}
}
Chineses.java
package practice2_1_again;
public class Chineses {
TV homeTV;
void buyTV(TV tv) {
homeTV = tv;
}
void remoteControl(int m) {
homeTV.setChanner(m);
}
void seeTV() {
homeTV.showProgram();
System.out.println("用户买回的电视是在"+homeTV.getChannel()+"频道");
}
}
Example4_5.java
package practice2_1_again;
public class Example4_5 {
public static void main(String[] args) {
TV haierTV = new TV();
haierTV.setChanner(5);
System.out.println("卖给用户的haierTV目前的频道是"+haierTV.getChannel());
Chineses zhangsan = new Chineses();
zhangsan.buyTV(haierTV);
System.out.println("zhangsan开始看电视节目");
zhangsan.seeTV();
int m = 2;
System.out.println("zhangsan用遥控器将买回的电视更改到"+m+"频道");
zhangsan.remoteControl(m);
System.out.println("现在卖给用户的haierTV目前的频道是"+haierTV.getChannel());
System.out.println("zhangsan再看电视节目");
zhangsan.seeTV();
}
}
Fractions.java
package practice2_2;
public class Fractions {
public int fson;
public int fmom;
//求分子分母的最大公约数
public int gcd(int a,int b) {
if(a < b) {
int c = a;
a = b;
b = c;
}
int r = a % b;
while(r != 0) {
a = b;
b = r;
r = a % b;
}
return b;
}
public void fAdd(int fson1,int fmom1,int fson2,int fmom2) {
int fm = fmom1*fmom2;
fson1 = fson1*fmom2;
fson2 = fson2*fmom1;
int fs = fson1+fson2;
int g = gcd(fs, fm);
System.out.println("最大公约数:"+g);
fs = fs / g;
fm = fm / g;
System.out.println("加法运算后结果中分子为:"+fs);
System.out.println("加法运算后结果中分母为:"+fm);
System.out.println("即分数为:"+fs+"/"+fm);
}
public void fSub(int fson1,int fmom1,int fson2,int fmom2) {
int fm = fmom1*fmom2;
fson1 = fson1*fmom2;
fson2 = fson2*fmom1;
int fs = fson1-fson2;
int g = gcd(fs, fm);
fs = fs / g;
fm = fm / g;
System.out.println("减法运算后结果中分子为:"+fs);
System.out.println("减法运算后结果中分母为:"+fm);
System.out.println("即分数为:"+fs+"/"+fm);
}
public void fMul(int fson1,int fmom1,int fson2,int fmom2) {
int fm = fmom1*fmom2;
int fs = fson1*fson2;
int g = gcd(fs, fm);
fs = fs / g;
fm = fm / g;
System.out.println("乘法运算后结果中分子为:"+fs);
System.out.println("乘法运算后结果中分母为:"+fm);
System.out.println("即分数为:"+fs+"/"+fm);
}
public void fDiv(int fson1,int fmom1,int fson2,int fmom2) {
int fm = fmom1*fson2;
int fs = fson1*fmom2;
int g = gcd(fs, fm);
fs = fs / g;
fm = fm / g;
System.out.println("除法运算后结果中分子为:"+fs);
System.out.println("除法运算后结果中分母为:"+fm);
System.out.println("即分数为:"+fs+"/"+fm);
}
}
Test2_2.java
package practice2_2;
import java.util.Scanner;
public class Test2_2 {
public static void main(String[] args) {
Fractions fractions = new Fractions();
@SuppressWarnings("resource")
Scanner inTo = new Scanner(System.in);
int fson1 = 0;
int fson2 = 0;
int fmom1 = 0;
int fmom2 = 0;
System.out.println("请输入第一个分数的分子:");
fson1 = inTo.nextInt();
System.out.println("请输入第一个分数的分母:");
fmom1 = inTo.nextInt();
System.out.println("请输入第二个分数的分子:");
fson2 = inTo.nextInt();
System.out.println("请输入第二个分数的分母:");
fmom2 = inTo.nextInt();
System.out.println("运算...");
fractions.fAdd(fson1, fmom1, fson2, fmom2);
fractions.fSub(fson1, fmom1, fson2, fmom2);
fractions.fMul(fson1, fmom1, fson2, fmom2);
fractions.fDiv(fson1, fmom1, fson2, fmom2);
}
}
属性包括:编号、姓名、年龄、职务、部门、出勤人数
方法包括:构造方法、输出信息的方法、签到方法
Employee.java
package practice3;
import java.util.Scanner;
public class Employee {
Scanner inTo = new Scanner(System.in);
public String[] num = new String[1024]; //编号 用数组来存储一组数据
public String[] name = new String[1024]; //姓名
public String[] age = new String[1024]; //年龄 //本来是int类型的,因为输入时会出问题,所以也换成了String
public String[] position = new String[1024]; //职务
public String[] department = new String[1024]; //部门
public int attendance_data = 0; //出勤人数
public Employee() {
//构造方法
}
public void inFormation() {
System.out.println("出勤人数:"+attendance_data);
for(int i = 0;i<attendance_data;i++) {
System.out.println("编号:"+num[i]);
System.out.println("姓名:"+name[i]);
System.out.println("年龄:"+age[i]);
System.out.println("职务:"+position[i]);
System.out.println("部门:"+department[i]);
}
}
public void sign() {
int i = 0;
/*System.out.println("请输入出勤人数:");
attendance_data = inTo.nextInt();*/
for(i = 0;i<2;i++) {
System.out.println("请输入编号:");
num[i] = inTo.nextLine();
System.out.println("请输入姓名:");
name[i] = inTo.nextLine();
System.out.println("请输入年龄:");
age[i] = inTo.nextLine();
System.out.println("请输入职务:");
position[i] = inTo.nextLine();
System.out.println("请输入部门:");
department[i] = inTo.nextLine();
System.out.println("如果你是最后一个签到的人请输入over,如果不是请输入1");
String flag = inTo.nextLine(); //这个和age的处境一样,不是String类型输入时会出问题
if(flag.equals("over")) break; //字符串比较需要用equals方法
}
attendance_data = i+1;
}
}
Test3.java
package practice3;
public class Test3 {
public static void main(String[] args) {
Employee employee = new Employee();
System.out.println("开始签到...");
employee.sign();
System.out.println("签到完毕!");
employee.inFormation();
}
}
提示:
•思考需要定义的类,例如:本程序需要用到学生、借书卡、书等对象,最后实现借书的过程,如果有指定的书,则输出“***借到了***书”,否则输出“没有借到书”。
•还需要认真思考每个类中有哪些属性和方法,能够更好的完成这个程序。
只能说,完全没找到超市购物的例子。。。
下面代码只是个表面
Student.java
package practice4;
public class Student {
public String studentName;
public Student(String studentName) {
this.studentName = studentName;
}
public String printName() {
return this.studentName;
}
}
Book.java
package practice4;
public class Book {
public String bookName;
public Book(String bookName) {
this.bookName = bookName;
}
public String printName() {
return this.bookName;
}
}
LibraryCard.java
package practice4;
public class LibraryCard {
public String privilege;
public LibraryCard(String privilege) {
this.privilege = privilege;
System.out.println("您的借书卡拥有权限:"+privilege);
}
public void printlnPrivilege() {
System.out.println("借书卡拥有权限:"+privilege);
}
}
Test4.java
package practice4;
public class Test4 {
public static void main(String[] args) {
Student student = new Student("HaoLong");
Book book = new Book("DataStructure");
LibraryCard libraryCard = new LibraryCard("Root privilege");
System.out.println("借书同学:"+student.printName());
System.out.println("要借书名称:"+book.printName());
System.out.println("借书需要权限:Root privilege");
libraryCard.printlnPrivilege();
if(libraryCard.privilege.equals("Root privilege"))
System.out.println(student.studentName+"借到了"+book.bookName);
else
System.out.println(student.studentName+"没借到"+book.bookName);
}
}