第一题
Book.class
public class Book {
String title;
Integer pageNum;
public void detail(String title, Integer pageNum) {
System.out.println("教材名称:" + title + "\n页数:" + pageNum+"\n");
}
Book(String title, Integer pageNum) {
this.title=title;
if (pageNum < 200) {
System.out.println("输入错误,页数少于200,赋予默认页数200.");
pageNum=200;
}
this.pageNum=pageNum;
this.detail(this.title,this.pageNum);
}
}
BookTest.class
public class BookTest {
public static void main(String[] args) {
Book book = new Book("语文", 30);
}
}
第二题
在上一章作业的基础上,改进Pig 类,可以完成
(1) 可以增加自己的体重/减轻自己的体重。
(2) 可以打印金字塔,层数随意,其实对前面写在主方法中的代码用方法进行封装。
Pig.class
public class Pig {
String name;
Double ti;
Integer age;
String colour;
public String toString() {
return name+"\t"+ti+"kg"+"\t"+age+"\t"+colour;
}
Pig(String name, Double ti, Integer age, String colour){
this.name= name;
this.ti=ti;
this.age=age;
this.colour=colour;
Scanner input = new Scanner(System.in);
System.out.println("请输入你要增/减的体重数(kg):");
double ti1 = input.nextDouble();// 输入 5,则是增加体重;输入 -5,则是减轻体重
this.ti += ti1;
}
public static void main(String[] args) {
Pig pig = new Pig("张三", 44.6, 23, "黄色");
System.out.println(pig);
}
}
Pyramid.class
public class Pyramid {
Integer line;
public void strangle(int a) {
for (int i = 1; i <=a; i++) {
for (int j = 0; j <a-i; j++) {
System.out.print(" ");
}
for (int h = 0; h < 2*i-1; h++) {
System.out.print("*");
}
System.out.println();
}
}
Pyramid(Integer line) {
this.line=line;
this.strangle(this.line);
}
public static void main(String[] args) {
Pyramid pyramid = new Pyramid(9);
}
}
第三题
请编写一个Cat 类,要求如下:
(a)该猫可以做四则运算,
(b)也可以进行面积计算。
如图:
将四则运算器和面积运算器合二为一,作一个运算器,主菜单让用户选择是做四则运算还是面积运算,分为两个子菜单,让用户选择加减乘除或者形状。如下界面:
Cat.class
public class Cat {
public void cat() {
for (int z = 1; z < 2; z++) {
System.out.println("请输入种类:\n1.四则运算\n2.计算面积\n");
Scanner input = new Scanner(System.in);
int sout = input.nextInt();
if (sout == 1) {
System.out.println("1.加法\n2.减法\n3.乘法\n4.除法\n");
int x = input.nextInt();
System.out.println("请输入两个运算数:");
int a = input.nextInt();
int b = input.nextInt();
switch (x) {
case 1:
System.out.println("a+b=" + (a + b));
System.out.println("是否要再次输入:yes or no ");
String aaa1 = input.next();
if ("yes".equals(aaa1)) {
z--;
} else if ("no".equals(aaa1)) {
break;
} else {
for (int q = 0; q < 1; q++) {
System.out.println("输入错误,是否要重新输入:yes or no");
String next = input.next();
if ("yes".equals(next)) {
z--;
} else if ("no".equals(next)) {
break;
} else {
q--;
}
}
}
break;
case 2:
System.out.println("a-b=" + (a - b));
System.out.println("是否要再次输入:yes or no ");
String aaa2 = input.next();
if ("yes".equals(aaa2)) {
z--;
} else if ("no".equals(aaa2)) {
break;
} else {
for (int q = 0; q < 1; q++) {
System.out.println("输入错误,是否要重新输入:yes or no");
String next = input.next();
if ("yes".equals(next)) {
z--;
} else if ("no".equals(next)) {
break;
} else {
q--;
}
}
}
break;
case 3:
System.out.println("a*b=" + (a * b));
System.out.println("是否要再次输入:yes or no ");
String aaa3 = input.next();
if ("yes".equals(aaa3)) {
z--;
} else if ("no".equals(aaa3)) {
break;
} else {
for (int q = 0; q < 1; q++) {
System.out.println("输入错误,是否要重新输入:yes or no");
String next = input.next();
if ("yes".equals(next)) {
z--;
} else if ("no".equals(next)) {
break;
} else {
q--;
}
}
}
break;
case 4:
System.out.println("a/b=" + (a / b));
System.out.println("是否要再次输入:yes or no ");
String aaa4 = input.next();
if ("yes".equals(aaa4)) {
z--;
} else if ("no".equals(aaa4)) {
break;
} else {
for (int q = 0; q < 1; q++) {
System.out.println("输入错误,是否要重新输入:yes or no");
String next = input.next();
if ("yes".equals(next)) {
z--;
} else if ("no".equals(next)) {
break;
} else {
q--;
}
}
}
break;
}
} else if (sout == 2) {
System.out.println("1.三角形\n2.正方形\n3.矩形\n4.圆形\n");
int y = input.nextInt();
switch (y) {
case 1:
System.out.println("请输入三角形的底和高:");
int c = input.nextInt();
int d = input.nextInt();
System.out.println("三角形的面积为:");
System.out.println((c * d) / 2.0);
System.out.println("是否要再次输入:yes or no ");
String aaa5 = input.next();
if ("yes".equals(aaa5)) {
z--;
} else if ("no".equals(aaa5)) {
break;
} else {
for (int q = 0; q < 1; q++) {
System.out.println("输入错误,是否要重新输入:yes or no");
String next = input.next();
if ("yes".equals(next)) {
z--;
} else if ("no".equals(next)) {
break;
} else {
q--;
}
}
}
break;
case 2:
System.out.println("请输入正方形的长和宽:");
double e = input.nextDouble();
double f = input.nextDouble();
System.out.println("正方形的面积为:");
System.out.println(e * f);
System.out.println("是否要再次输入:yes or no ");
String aaa6 = input.next();
if ("yes".equals(aaa6)) {
z--;
} else if ("no".equals(aaa6)) {
break;
} else {
for (int q = 0; q < 1; q++) {
System.out.println("输入错误,是否要重新输入:yes or no");
String next = input.next();
if ("yes".equals(next)) {
z--;
} else if ("no".equals(next)) {
break;
} else {
q--;
}
}
}
break;
case 3:
System.out.println("请输入矩形的长和宽:");
double g = input.nextDouble();
double h = input.nextDouble();
System.out.println("矩形的面积为:");
System.out.println(g * h);
System.out.println("是否要再次输入:yes or no ");
String aaa7 = input.next();
if ("yes".equals(aaa7)) {
z--;
} else if ("no".equals(aaa7)) {
break;
} else {
for (int q = 0; q < 1; q++) {
System.out.println("输入错误,是否要重新输入:yes or no");
String next = input.next();
if ("yes".equals(next)) {
z--;
} else if ("no".equals(next)) {
break;
} else {
q--;
}
}
}
break;
case 4:
System.out.println("请输入圆形的半径:");
double r = input.nextDouble();
double s = Math.sqrt(r) * Math.PI;
System.out.println("圆形的面积为:");
System.out.println(s);
System.out.println("是否要再次输入:yes or no ");
String aaa8 = input.next();
if ("yes".equals(aaa8)) {
z--;
} else if ("no".equals(aaa8)) {
break;
} else {
for (int q = 0; q < 1; q++) {
System.out.println("输入错误,是否要重新输入:yes or no");
String next = input.next();
if ("yes".equals(next)) {
z--;
} else if ("no".equals(next)) {
break;
} else {
q--;
}
}
}
break;
}
} else {
System.out.println("输入错误!!!");
z--;
}
}
}
Cat() {
this.cat();
}
public static void main (String[]args){
Cat cat = new Cat();
}
}
第四题
SdCard.class
public class SdCard {
final int capacity=10;
String [] data;
public String seek(String songName){
for (int i = 0; i < this.data.length; i++) {
if (songName.equals(this.data[i])) {
return this.data[i];
}
}
return null;
}
public void write(String songName) {
for (int i = 0; i < data.length; i++) {
if (this.data[i] == null) {
this.data[i]=songName;
break;
}
}
}
public String toString() {
return "当前歌曲:"+ Arrays.toString(this.data);
}
SdCard() {
this.data=new String[capacity];
}
SdCard(int capacity) {
this.data = new String[capacity];
}
}
Phone.class
public class Phone {
String branch;
Cell cell;
SdCard sdCard;
Phone(String branch,Cell cell,SdCard sdCard) {
this.branch=branch;
this.cell=cell;
this.sdCard=sdCard;
}
public void downlaod(String music) {
this.sdCard.write(music);
}
public void playMusic(String songName) {
this.sdCard.seek(songName);
}
public void cell(int electricity) {
this.cell.charging(electricity);
}
public void display() {
System.out.println(this.sdCard);
}
public String readSdCard(String songName) {
return this.sdCard.seek(songName);
}
public void writeSdCard(String songName) {
this.sdCard.write(songName);
}
}
Cell.class
public class Cell {
String brand;
int electricity;
Cell(String brand,int electricity) {
this.brand=brand;
this.electricity=electricity;
this.charging(this.electricity);
}
public int charging(int electricity) {
this.electricity=electricity;
Random random = new Random();
System.out.println("当前电量:"+this.electricity);
while(this.electricity!=100) {
int ii = random.nextInt(1,101 - this.electricity);
this.electricity += ii;
System.out.println("当前电量:"+this.electricity);
}
return this.electricity;
}
}
Test.class
public class Test {
public static void main(String[] args) {
Phone phone = new Phone("华为", new Cell("三星", 45),
new SdCard(5));
phone.writeSdCard("小城故事");
phone.writeSdCard("渡口");
System.out.println(phone.sdCard);
phone.playMusic("渡口");
Scanner scanner = new Scanner(System.in);
System.out.print("请输入下载的歌曲:");
String song = scanner.nextLine();
phone.downlaod(song);
phone.display();
}
}
第五题
编写以下两个方法:
(1)验证身份证号;
(2) 将身份证号中的出生日期转换成java.util.Date 对象返回。
/**
* 编写一个方法实现身份证号的验证
* @param idNo 身份证号
* @return true:有效; false:无效
*/
/**
* 通过身份证号获得出生日期
* @param idcardNo 身份证号
*/
注:日期格式,区分大小写:yyyy(小写)表示年分、MM(大写)表示月份、dd(小写)表示日、HH表示24小时制、hh表示12小时制、ss表示秒
IdCard.class
public class IdCard {
String idNo;
public void validldNo(String idNo) {
String regex = "^(\\d{6})(\\d{4})(\\d{2})(\\d{2})(\\d{3})([0-9]|X)$";
if (idNo.matches(regex)) {
System.out.println("身份证号有效!");
} else{
System.out.println("无效。");
}
}
public Date getBirthday(String idNo) {
Date parse=null;
try {
String substring = idNo.substring(6, 14);
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd");
parse = simpleDateFormat.parse(substring);
} catch (ParseException e) {
e.printStackTrace();
}
return parse;
}
IdCard() {
}
public static void main(String[] args) {
IdCard idCard = new IdCard();
idCard.validldNo("153276200012111111");
idCard.getBirthday("642926200012220000");
}
}
第六题
编写一个方法参数接收一个Date类型的参数,将参数Date格式化成以下格式的字符串输出。
- 2012年07月20日 14:23:45的格式进行输出。
- 2012-07-20 14:23:45的格式进行输出
DateDemo.class
public class DateDemo {
public String formatString(Date date) {
String pattern = "yyyy-MM-dd HH:mm:ss";
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern);
String format = simpleDateFormat.format(date);
return format;
}
public String formatString1(Date date) {
String pattern = "yyyy年MM月dd日 HH:mm:ss";
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern);
String format = simpleDateFormat.format(date);
return format;
}
public static void main(String[] args) {
DateDemo dateDemo = new DateDemo();
Date date = new Date();
String s =dateDemo.formatString(date);
System.out.println(s);
String s1 = dateDemo.formatString1(date);
System.out.println(s1);
}
}
第七题
使用Calendar实现万年历
PerpetualCalendar.class
public class PerpetualCalendar {
static Scanner input=new Scanner(System.in);
public void calendars(int year,int month) {
int day=1;
System.out.println("星期日\t星期一\t星期二\t星期三\t星期四\t星期五\t星期六");
Calendar instance = Calendar.getInstance();
instance.set(year,month-1,day);
int actualMaximum = instance.getActualMaximum(Calendar.DAY_OF_MONTH);
int i = instance.get(Calendar.DAY_OF_WEEK);
switch (i) {
case 1:break;
case 2:System.out.print("\t\t");
break;
case 3: System.out.print("\t\t\t\t");
break;
case 4:System.out.print("\t\t\t\t\t\t");
break;
case 5:System.out.print("\t\t\t\t\t\t\t\t");
break;
case 6:System.out.print("\t\t\t\t\t\t\t\t\t\t");
break;
case 7:System.out.print("\t\t\t\t\t\t\t\t\t\t\t\t");
break;
}
for (int j = 1; j <= actualMaximum; j++) {
System.out.print(j+"\t\t");
if (i == 1) {
if (j % 7 == 0) {
System.out.println();
}
}
if (i == 2) {
if (j == 6||(j-6)%7==0) {
System.out.println();
}
}
if (i == 3) {
if (j == 5 || (j - 5) % 7 == 0) {
System.out.println();
}
}
if (i == 4) {
if (j == 4||(j-4)%7==0) {
System.out.println();
}
}
if (i == 5) {
if (j == 3||(j-3)%7==0) {
System.out.println();
}
}
if (i == 6) {
if (j == 2||(j-2)%7==0) {
System.out.println();
}
}
if (i == 7) {
if ((j-1)%7==0) {
System.out.println();
}
}
}
}
public static void main(String[] args) {
System.out.println("请输入年份:");
int year = input.nextInt();
System.out.println("请输入月份:");
int month = input.nextInt();
PerpetualCalendar perpetualCalendar = new PerpetualCalendar();
perpetualCalendar.calendars(year,month);
}
}
第八题
编写一个存储器示例,存储器有list方法、读read和write方法可以读写文件(File类)。Read方法通过文件名可以得到File对象,write方法可将一个文件存储在files数组中,其类图结构如下。
存储器:Storage |
//属性 File[ ]:files 其它属性..…. |
读取:read(String fileName); 写:writer(File file); list()方法显示存储器中所有文件的信息; |
文件类:File |
属性….. |
toString()方法 |
Storage.class
public class Storage {
Files[] files= new Files[10];
static Scanner input=new Scanner(System.in);
Storage() {
}
void list() {
for (int i = 0; i < files.length; i++) {
if (files[i] == null) {
continue;
}
System.out.println("文件名字:"+files[i].name+"\t大小:"+files[i].size);
}
}
void write() {
System.out.println("请输入文件名字:");
String next = input.next();
System.out.println("请输入大小:");
int next1 = input.nextInt();
for (int i = 0; i < files.length; i++) {
if (files[i] == null) {
files[i] = new Files(next, next1);
break;
}
}
}
void read() {
System.out.println("请输入文件名字来查找:");
String next = input.next();
for (int i = 0; i < files.length; i++) {
if (files[i] == null) {
continue;
}
else {
if (next.equals(files[i].name)) {
System.out.println("文件名字:" + files[i].name + "\t大小:" + files[i].size);
break;
}
else {
System.out.println("无此文件。");
break;
}
}
}
}
}
Files.class
public class Files {
int size;
String name;
Files(String name,int size) {
this.name=name;
this.size=size;
}
}