July14th.2020 homework
Ps:好久没写blog,所以打算写一个作业练习的系列,可以让新手更容易上手。大家加油!!废话不多说,上代码。
练习1
package july14th;
public class Subject1 {
public static void main(String[] args) {
String []brand={"Nike背包","Adidas运动衫","李宁运动鞋","Kappa外套","361°腰包"};
for(int i=0;i<brand.length;i++){
System.out.println(brand[i]);
}
}
}
练习2
package july14th;
import java.util.Scanner;
public class Subject2 {
public static void main(String[] args) {
double money[]=new double[5];
Scanner sc=new Scanner(System.in);
System.out.println("请输入会员本月的消费记录");
for(int i=0;i<money.length;i++){
System.out.println("请输入第"+(i+1)+"笔购物金额");
money[i]=sc.nextDouble();
}
System.out.println("");
int sum=0;
System.out.println("序号 "+"金额(元)");
for(int i=0;i<money.length;i++){
System.out.println((i+1)+" "+money[i]);
sum+=money[i];
}
System.out.println("总金额 "+sum);
}
}
练习3
package july14th;
import java.util.Arrays;
import java.util.Scanner;
public class Subject3 {
public static void main(String[] args) {
Scanner scores=new Scanner(System.in);
System.out.println("请输入5位学员的成绩(整数)");
int[]student=new int[5];
for(int i=0;i<student.length;i++){
student[i]=scores.nextInt();
}
Arrays.sort(student);
System.out.print("学员成绩按升序排列:");
for(int i=0;i<student.length;i++){
System.out.print(student[i]+" ");
}
}
}
练习4
package july14th;
import java.util.Arrays;
public class Subject4 {
public static void main(String[] args) {
char[]character={'a','c','u','b','e','p','f','z'};
System.out.print("原字符序列:");
for(int i=0;i<character.length;i++){
System.out.print(character[i]+" ");
}
System.out.print("
升序排序后:");
Arrays.sort(character);
for(int i=0;i<character.length;i++){
System.out.print(character[i]+" ");
}
System.out.print("
降序排序后:");
for(int i=0 ;i<character.length;i++){
for(int j=character.length-1;j>=i+1;j--){
if(character[j]>character[j-1]){
int temp=character[j-1];
character[j-1]=character[j];
character[j]=(char) temp;
}
}
}
for(int i=0;i<character.length;i++){
System.out.print(character[i]+" ");
}
}
}
练习5
package july14th;
import java.util.Scanner;
public class Subject5 {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("请输入4家店的价格");
int price []=new int[4];
for(int i=0;i<price.length;i++){
System.out.print("第"+(i+1)+"家店的价格:");
price[i]=sc.nextInt();
}
int min=price[0];
for(int i=0;i<price.length;i++){
if(min>price[i]){
min=price[i];
}
}
System.out.println("最低价格是"+min);
}
}
July20th.2020 homework
睡觉前把下午完成的作业写到blog,好困。。。要是能有个人给我点个赞我将会无比高兴!!
练习6
package july20th;
import java.util.Scanner;
public class Tourer {
String name;
int age;
String ticket;
public void info() {
ticket = age <= 12 ? "免费" : "价格为:20元";
System.out.println(name + "的年龄为:" + age + ",门票" + ticket+"
");
}
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int age = 0;
String name = " ";
for (;;) {
Tourer person = new Tourer();
if (!name.equals("n")) {
System.out.print("请输入姓名:");
name = input.next();
if (name.equals("n")) {
System.out.println("退出程序");
break;
}
System.out.print("请输入年龄:");
age = input.nextInt();
person.name = name;
person.age = age;
person.info();
} else {
break;
}
}
}
}
练习7
package july20th;
import java.util.Scanner;
class Customer{
int mark;
String cardType;
public void showInfo(){
System.out.println("积分:"+mark+",卡类型:"+cardType);
if(mark>1000&&cardType.equals("金卡")||
mark>5000&&cardType.equals("普卡")){
System.out.println("回馈积分500分!");
}
}
}
public class CustomerTest {
public static void main(String[] args) {
Scanner input=new Scanner(System.in);
Customer test=new Customer();
System.out.print("请输入您的信用卡积分:");
test.mark=input.nextInt();
System.out.print("请输入您的信用卡类型:(金卡/普卡)");
test.cardType=input.next();
test.showInfo();
}
}
练习8
package july20th;
import java.util.Scanner;
public class Administrator {
String name;
int passWord;
public void adminInfo() {
System.out.println("姓名:" + name + ",密码:" + passWord);
}
public void modifyInfo() {
}
public static void main(String[] args) {
Administrator admin1 = new Administrator();
admin1.name = "admin1";
admin1.passWord = 111111;
admin1.adminInfo();
Administrator admin2 = new Administrator();
admin2.name = "admin2";
admin2.passWord = 222222;
admin2.adminInfo();
Administrator admin3 = new Administrator();
admin3.name = "admin3";
admin3.passWord = 333333;
Scanner input = new Scanner(System.in);
boolean isFlag = true;
while (isFlag) {
System.out.println("请输入用户名:");
String name = input.next();
System.out.println("请输入密码:");
int passWord = input.nextInt();
if (admin3.name.equals("admin3") && admin3.passWord == passWord) {
System.out.println("请输入新密码");
passWord = input.nextInt();
admin3.passWord = passWord;
System.out.println("密码修改成功,您的新密码为" + admin3.passWord);
isFlag = false;
} else {
System.out.println("用户名和密码不匹配!您没有权限更新管理员信息。");
break;
}
}
}
}
PS:以上只是我自己的解题方法,代码么,其实没有什么标准的死答案,只要写的代码能实现题目要求就好了,活学活用就好了。本菜鸡要去睡觉了Zzz…晚安
July21th.2020 homework
这次的作业除了第一题写的感觉比较完美以外,另外两个题感觉做的不尽如意,不过也算是照题目要求做出来了。。。
练习9
package july21th.pro.demo1;
import java.util.Scanner;
public class Menu {
Scanner input = new Scanner(System.in);
public void loginMenu() { // 登录
for (;;) {
System.out.println("欢迎使用我行我素购物管理系统
");
System.out.println(" 1.登陆系统
");
System.out.println(" 2.退出
");
System.out.println("***************************");
System.out.print("请选择,输入数字:");
int choose = input.nextInt();
if (choose == 1) {
mainMenu();
break;
} else if (choose == 2) {
System.out.println("谢谢使用");
break;
} else {
System.out.println("输入错误,请重新输入!");
}
}
}
public void mainMenu() { // 主菜单
for (;;) {
System.out.println("我行我素购物管理系统主菜单
");
System.out.println("***************************
");
System.out.println(" 1.客户信息管理
");
System.out.println(" 2.真情回顾
");
System.out.println("***************************");
System.out.print("请选择,输入数字或按0返回上一级菜单:");
int choose = input.nextInt();
if (choose == 1) {
customerMenu();
break;
} else if (choose == 2) {
sendCustMenu();
break;
} else if (choose == 0) {
loginMenu();
break;
} else {
System.out.println("输入错误,请重新输入!");
}
}
}
public void customerMenu() { // 客戶信息管理
for (;;) {
System.out.println("我行我素购物管理系统主菜单>客户信息管理");
System.out.println("***************************
");
System.out.println(" 1.添加客户
");
System.out.println(" 2.修改客户
");
System.out.println(" 3.删除客户
");
System.out.println("***************************");
System.out.print("请选择,输入数字或按0返回上一级菜单:");
int choose = input.nextInt();
if (choose == 1) {
for (;;) {
System.out.println("执行添加客户");
System.out.print("输入数字0返回上一级菜单:");
int choose1 = input.nextInt();
if (choose1 == 0) {
customerMenu();
break;
} else {
System.out.println("输入错误,输入数字0返回上一级菜单!");
}
}
break;
} else if (choose == 2) {
for (;;) {
System.out.println("执行修改客户");
System.out.print("输入数字0返回上一级菜单:");
int choose1 = input.nextInt();
if (choose1 == 0) {
customerMenu();
break;
} else {
System.out.println("输入错误,输入数字0返回上一级菜单!");
}
}
break;
} else if (choose == 3) {
for (;;) {
System.out.println("执行删除客户");
System.out.print("输入数字0返回上一级菜单:");
int choose1 = input.nextInt();
if (choose1 == 0) {
customerMenu();
break;
} else {
System.out.println("输入错误,输入数字0返回上一级菜单!");
}
}
break;
} else if (choose == 0) {
mainMenu();
break;
} else {
System.out.println("输入错误,请重新输入!");
}
}
}
public void sendCustMenu() { // 回饋顧客
for (;;) {
System.out.println("我行我素购物管理系统主菜单>");
System.out.println("***************************
");
System.out.println(" 1.幸运大放送
");
System.out.println(" 2.幸运抽奖
");
System.out.println(" 3.生日问候
");
System.out.println("***************************");
System.out.print("请选择,输入数字或按0返回上一级菜单:");
int choose = input.nextInt();
if (choose == 1) {
for (;;) {
System.out.println("执行幸运大放送");
System.out.print("输入数字0返回上一级菜单:");
int choose1 = input.nextInt();
if (choose1 == 0) {
customerMenu();
break;
} else {
System.out.println("输入错误,输入数字0返回上一级菜单!");
}
}
break;
} else if (choose == 2) {
for (;;) {
System.out.println("执行幸运抽奖");
System.out.print("输入数字0返回上一级菜单:");
int choose1 = input.nextInt();
if (choose1 == 0) {
customerMenu();
break;
} else {
System.out.println("输入错误,输入数字0返回上一级菜单!");
}
}
break;
} else if (choose == 3) {
for (;;) {
System.out.println("执行生日问候");
System.out.print("输入数字0返回上一级菜单:");
int choose1 = input.nextInt();
if (choose1 == 0) {
customerMenu();
break;
} else {
System.out.println("输入错误,输入数字0返回上一级菜单!");
}
}
break;
} else if (choose == 0) {
mainMenu();
} else {
System.out.println("输入错误,请重新输入!");
}
}
}
}
package july21th.pro.demo1;
public class MenuTest {
public static void main(String[] args) {
Menu menu=new Menu();
menu.loginMenu();
}
}
练习10
人机猜拳(石头剪刀布)
package july21th.pro.demo2;
import java.util.Scanner;
public class Game {
int computer;
int self;
public int selfGuss() { // 玩家出拳
Scanner input = new Scanner(System.in);
System.out.println("请您开始出拳:1-石头、2-剪刀、3-布");
int self = input.nextInt();
for (;;) {
if (self == 1) {
System.out.println("您选择出拳: 石头");
break;
} else if (self == 2) {
System.out.println("您选择出拳: 剪刀");
break;
} else if (self == 3) {
System.out.println("您选择出拳: 布");
break;
}
}
return self;
}
public int computerGuss() { // 电脑出拳
int computer = (int) (Math.random() * 3 + 1);
for (;;) {
if (computer == 1) {
System.out.println("电脑选择出拳: 石头");
break;
} else if (computer == 2) {
System.out.println("电脑选择出拳: 剪刀");
break;
} else if (computer == 3) {
System.out.println("电脑选择出拳: 布");
break;
}
}
return computer;
}
public void compare() { //判断谁赢
self = selfGuss();
computer = computerGuss();
if (computer == 1 && self == 2 || computer == 2 && self == 3
|| computer == 3 && self == 1) {
System.out.println("电脑赢!!");
} else if (computer == self) {
System.out.println("平手!!");
} else {
System.out.println("恭喜,您赢了!");
}
}
}
package july21th.pro.demo2;
import java.util.Scanner;
public class GameTest {
public static void main(String[] args) {
Game finGuss=new Game();
Scanner input = new Scanner(System.in);
System.out.println("********猜拳游戏********
");
System.out.println("请输入您的角色名");
String name = input.next();
System.out.println("请选择您的对手:1-孙悟空 2-孙悟饭 3-孙悟天");
for (;;) {
int person = input.nextInt();
if (person == 1) {
System.out.println("***********"+name+"vs"+"孙悟空"+"***********");
break;
} else if (person == 2) {
System.out.println("***********"+name+"vs"+"孙悟饭"+"***********");
break;
} else if (person == 3) {
System.out.println("***********"+name+"vs"+"孙悟天"+"***********");
break;
} else {
System.out.println("没有此角色,请重新输入!");
}
}
finGuss.compare();
}
}
练习11
三个类分别是银行卡 类 ,功能类 ,测试类 。属性 用户名 密码 余额功能 类 登录 显示余额, 存 取关系 功能类中成员变量是银行卡类。
package july21th.pro.demo3;
public class BankCard {
String name;
int passWord;
int balance;
}
package july21th.pro.demo3;
import java.util.Scanner;
public class Utility {
BankCard card=new BankCard();
Scanner input=new Scanner(System.in);
public void login(){ //登录
System.out.println("******中国建设银行atm******");
System.out.println("1.登录");
System.out.println("2.退出");
System.out.print("请选择");
for(;;){
int choose=input.nextInt();
if(choose==1){
money();
}else if(choose==2){
System.out.println("谢谢使用");
break;
}else{
System.out.println("输入错误,请重新选择!");
}
}
}
public void money(){ //主菜单
card.name="张三";
card.passWord=123456;
System.out.println("请输入用户名:");
String name=input.next();
System.out.println("请输入密码:");
int passWord=input.nextInt();
if(name.equals("张三")&&card.passWord==passWord){
System.out.println("******中国建设银行atm******");
System.out.println("1.显示余额");
System.out.println("2.开始存钱");
System.out.println("3.开始取钱");
System.out.println("请选择");
for(;;){
int choose=input.nextInt();
if(choose==1){
card.balance=2000;
System.out.println("余额为:"+card.balance);
return;
}else if(choose==2){
addMoney();
return;
}else if(choose==3){
subMoney();
return;
}else if(choose==0){
System.out.println("按0返回上级菜单");
login();
}else{
System.out.println("选择错误,请重新输入");
}
}
}else{
System.out.println("用户名或者密码错误!!");
}
}
public int addMoney(){ //存钱
System.out.println("请输入存入的钱数:");
int money=input.nextInt();
System.out.println("您存入的钱为:"+money);
return money;
}
public int subMoney(){
System.out.println("请输入要取的钱数:");
int money=input.nextInt();
System.out.println("您要取的钱为:"+money);
return money;
}
}
package july21th.pro.demo3;
public class BankCardTest {
public static void main(String[] args) {
Utility uti=new Utility();
uti.login();
}
}
不知道什么时候才能恢复线下课啊,该死的新冠,2020,我可太难了啊!!
July24th.2020 homework
这次作业感觉完成的比较满意,虽然完成慢一点。。
练习12
自定义方法 :两个 int参数 , 返回 随机数int。
package july24th.demo1;
import java.util.Scanner;
public class RandomTest {
public static void main(String[] args) {
Random num = new Random();
Scanner input = new Scanner(System.in);
System.out.println("请输入第一个整数:");
int a = input.nextInt();
System.out.println("请输入第二个整数:");
int b = input.nextInt();
System.out.println(num.getRandom(a, b));
}
}
class Random {
public int getRandom(int a, int b) {
int ran = (int) (Math.random() * (b - a + 1) + a);
return ran;
}
}
任意两个整数之间的随机数:
(int)(Math.random()*(b-a+1)+a;
练习13
自定义方法 ,两个参数1 String[] 2String name 返回 找到的下标。
package july24th.demo2;
public class NameIndex {
public int getindex(String[] arr, String name) {
for (int i = 0; i < arr.length; i++) {
if (name.equals(arr[i])) {
return i;
}
}
return -1;
}
}
package july24th.demo2;
import java.util.Scanner;
public class NameIndexTest {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
NameIndex index = new NameIndex();
String[] name = new String[] { "Jack", "Lilly", "Tom", "Benz" };
for (String i : name) {
System.out.print(i + " ");
}
System.out.println("
");
System.out.println("请输入你要查找的名字");
String person = input.next();
int index1 = index.getindex(name, person);
if (index1 >= 0) {
System.out.println("恭喜您,找到了,在第" + (index1 + 1) + "位");
} else {
System.out.println("很遗憾,数据库没有这个人!");
}
}
}
练习14
ATM系统,登录,显示余额、存钱、取钱、退出功能。
package july24th.demo3;
public class Bank {
int account;
int passWord;
double balance;
}
package july24th.demo3;
import java.util.Scanner;
public class ATM {
Scanner input = new Scanner(System.in);
Bank b = new Bank();
public void info() { // 初始化
b.account = 888888;
b.passWord = 123456;
b.balance = 500;
}
public void main() {
System.out.println("1.显示余额");
System.out.println("2.我要存钱");
System.out.println("3.我要取钱");
System.out.println("4.退出");
for (;;) {
System.out.println("请选择功能:");
int choose = input.nextInt();
if (choose == 1) {
showMoney();
} else if (choose == 2) {
System.out.println("请输入要存入的钱");
deposit(input.nextDouble());
} else if (choose == 3) {
System.out.println("请输入要取出的钱");
withdraw(input.nextDouble());
} else if (choose == 4) {
System.out.println("谢谢使用");
break;
} else {
System.out.println("没有此选项,请重新输入!");
}
}
}
public boolean login(int acc, int pass) { // 校验账户密码
if (acc == b.account && pass == b.passWord) {
return true;
}
return false;
}
public void showMoney() { // 账户余额
System.out.println("您的余额为" + b.balance);
}
public void deposit(double amount) { // 存钱
if (b.balance > 0) {
b.balance += amount;
}
System.out.println("存钱成功,您当前余额为" + b.balance);
}
public void withdraw(double amount) { // 取钱
if (b.balance < amount) {
System.out.println("对不起,您的余额不足!");
} else {
b.balance -= amount;
System.out.println("取钱成功,您当前余额为" + b.balance);
}
}
}
package july24th.demo3;
import java.util.Scanner;
public class ATMTest {
public static void main(String[] args) {
System.out.println("**********中国建设银行**********");
Scanner input = new Scanner(System.in);
ATM a = new ATM();
a.info();
for (int i = 4; i > 0; i--) {
System.out.println("请输入账号");
int account = input.nextInt();
System.out.println("请输入密码");
int passWord = input.nextInt();
boolean num = a.login(account, passWord);
if (num) {
System.out.println("登录成功");
a.main();
break;
} else {
System.out.println("账号或密码错误" + "您还有" + (i - 1) + "次机会");
}
}
}
}
练习15
自定义数组 ,参数 1 数组 2 字符串 返回数组 int
参数1 无序数组 , 根据参数2 ,方法中 判断 返回升序或降序后的数组
package july24th.demo4;
import java.util.Scanner;
public class BubbleTest {
public static void main(String[] args) {
Bubble test = new Bubble();
int[] ron = new int[10];
for (int i = 0; i < ron.length; i++) {
ron[i] = (int) (Math.random() * 100 + 1);
}
for (int i : ron) {
System.out.print(i + " ");
}
Scanner input = new Scanner(System.in);
System.out.println(" ");
for (;;) {
System.out.println("请问您是升序还是降序?(up/down)");
int[] arr = ron;
test.sort(arr, input.next());
for (int i : arr) {
System.out.print(i + " ");
}
System.out.println(" ");
System.out.println("请问是否继续(Y/N)");
String s = input.next();
if (s.equals("Y")) {
} else {
System.out.println("谢谢使用!");
break;
}
}
}
}
class Bubble {
public int[] sort(int[] arr, String choose) {
if (choose.equals("up")) {
for (int i = 0; i < arr.length - 1; i++) {
for (int j = 0; j < arr.length - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
return arr;
} else {
for (int i = 0; i < arr.length - 1; i++) {
for (int j = 0; j < arr.length - i - 1; j++) {
if (arr[j] < arr[j + 1]) {
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
return arr;
}
}
}
End.
July27th.2020 homework
练习16
package july27th.demo2;
public class Excelle {
private String type;
private String id;
public Excelle(String id,String type){
this.id=id;
this.type=type;
}
public String getType() {
return type;
}
public String getId() {
return id;
}
}
package july27th.demo2;
public class Regal {
private String type;
private String id;
public Regal(String id,String type){
this.id=id;
this.type=type;
}
public String getType() {
return type;
}
public String getId() {
return id;
}
}
package july27th.demo2;
public class Seller {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public void sell(Excelle car){
System.out.println(car.getId()+" "+car.getType());
}
public void sell(Regal car){
System.out.println(car.getId()+" "+car.getType());
}
public void sell(Excelle car,int num) {
System.out.println(name+" "+num+"辆"+"编号:"+car.getId()+"品牌:"+car.getType());
}
}
package july27th.demo2;
public class Test {
public static void main(String[] args) {
Seller name=new Seller();
name.setName("jack");
Excelle e=new Excelle("1","凯越");
name.sell(e, 100);
name.sell(e);
}
}
练习17
package july27th.demo1;
public class Dog {
String nickName;
private int health;
int love;
public Dog(){
}
public Dog(String nickName, int health, int love) {
this.nickName = nickName;
this.health = health;
this.love = love;
}
public void setHealth(int health){
if(health<=100&&health>=0){
this.health=health;
}else{
this.health=50;
}
}
public int getHealth(){
return health;
}
public void print() {
System.out.println("我的名字叫" + nickName + ",健康值是" + health + ",和主人的亲密度是"
+ love );
}
}
package july27th.demo1;
public class Penguin {
String nickName;
private int health;
int love;
public Penguin(){
}
public Penguin(String nickName, int health, int love) {
this.nickName = nickName;
this.health = health;
this.love = love;
}
public void setHealth(int health){
if(health<=100&&health>=0){
this.health=health;
}else{
this.health=50;
}
}
public int getHealth(){
return health;
}
public void print() {
System.out.println("我的名字叫" + nickName + ",健康值是" + health + ",和主人的亲密度是"
+ love );
}
}
package july27th.demo1;
import java.util.Scanner;
public class PetsTest {
public static void main(String[] args) {
System.out.println("欢迎来到宠物店");
Scanner input=new Scanner(System.in);
System.out.print("请输入要领养的宠物名字:");
String name=input.next();
System.out.println("请选择要领养的宠物类型:(1、狗狗 2、企鹅)");
int choose=input.nextInt();
if(choose==1){
System.out.println("请选择狗狗的品种:(1、聪明的拉布拉多犬 2、酷酷的雪瑞纳)");
Dog d=new Dog();
int n=input.nextInt();
d.setHealth(-50);
Dog d2=new Dog(name,d.getHealth(),0);
String s=n==1? "聪明的拉布拉多犬":"酷酷的雪瑞纳";
System.out.println("宠物的自白:");
System.out.println("我的名字叫" + name + ",健康值是" + d2.getHealth() + ",和主人的亲密度是"
+ d.love + ",我是一只" + s);
}else if(choose==2){
System.out.println("请选择企鹅的性别:(1、Q仔2、Q妹)");
Penguin d=new Penguin();
int f=input.nextInt();
d.setHealth(-50);
Penguin d2=new Penguin(name,d.getHealth(),0);
String s=f==1? "Q仔":"Q妹";
System.out.println("宠物的自白:");
System.out.println("我的名字叫" + name + ",健康值是" + d2.getHealth() + ",和主人的亲密度是"
+ d.love + ",性别是" + s);
}else{
System.out.println("输入错误,请重新输入");
}
}
}
End.
July28th.2020 homework
练习18
要求 :
1 父类水果类 没有属性, 有一个抽象方法 (结出水果,无返回,无参数)
2 香蕉 子类 重写结出水果 输出结出一个香蕉
3 苹果子类 重写结出水果 输出结出一个苹果
4 测试类 new 两个子类 调用结出的方法
package july28th.demo1;
public abstract class Fruit {
public abstract void fruit();
}
class Banana extends Fruit{
@Override
public void fruit() {
// TODO Auto-generated method stub
System.out.println("结出一个香蕉");
}
}
class Apple extends Fruit{
@Override
public void fruit() {
// TODO Auto-generated method stub
System.out.println("结出一个苹果");
}
}
package july28th.demo1;
public class FruitTest {
public static void main(String[] args) {
Banana b=new Banana();
b.fruit();
Apple a=new Apple();
a.fruit();
}
}
练习19
要求 :
1 父类披萨类 没有属性, 有一个生成披萨的方法 (生成披萨,String返回,无参数)
2 芝士披萨 子类 重写生成披萨的方法 输出结出一个芝士披萨
3 牛肉披萨 子类 重写生成披萨的方法 输出结出一个牛肉披萨
4 测试类 new 两个子类 调用结出的方法 注意有返回类型
package july28th.demo2;
public class Pizza {
public String pizza(){
String pizza="披萨";
System.out.println(pizza);
return pizza;
}
}
class CheesePizza extends Pizza{
@Override
public String pizza() {
// TODO Auto-generated method stub
String pizza="芝士披萨";
return pizza;
}
}
class BeefPizza extends Pizza{
@Override
public String pizza() {
// TODO Auto-generated method stub
String pizza="牛肉披萨";
return pizza;
}
}
package july28th.demo2;
public class PizzaTest {
public static void main(String[] args) {
CheesePizza cheese=new CheesePizza();
System.out.println("做一个"+cheese.pizza());
BeefPizza beef=new BeefPizza();
System.out.println("做一个"+beef.pizza());
}
}
练习20
package july28th.demo3;
public abstract class MotoVehicle {
int number;
String brand;
String color;
double mileAge;
public abstract void calcRent(int days);
}
class Car extends MotoVehicle {
String type;
public Car(int number, String type) {
this.number = number;
this.type = type;
}
@Override
public void calcRent(int days) {
// TODO Auto-generated method stub
if (type.equals("别克商务舱GL8")) {
System.out.println("租金是:" + days * 600+"元");
} else if (type.equals("宝马550i")) {
System.out.println("租金是:" + days * 500+"元");
} else if (type.equals("别克林荫大道")) {
System.out.println("租金是:" + days * 300+"元");
} else {
System.out.println("本公司无此车!");
}
}
}
class Bus extends MotoVehicle {
int seatCount;
public Bus(int number, int seatCount) {
this.number=number;
this.seatCount=seatCount;
}
@Override
public void calcRent(int days) {
// TODO Auto-generated method stub
if(seatCount>=16){
System.out.println("租金是:"+days*1500+"元");
}else{
System.out.println("租金是:"+days*800+"元");
}
}
}
package july28th.demo3;
import java.util.Scanner;
public class MotoVehicleTest {
public static void main(String[] args) {
for(;;){
System.out.println(" 拓海租车行
");
System.out.println("************************");
System.out.println("轿车:别克商务舱GL8(¥600/天)、宝马550i(¥500/天)、" +
"别克林荫大道(¥300/天)
");
System.out.println("客车:16座以上(¥1500/天)、16座以下(¥800/天)
");
Scanner input=new Scanner(System.in);
System.out.println("请问您要租什么类型的车?(1.轿车、2.客车)");
int choose=input.nextInt();
if(choose==1){
System.out.println("您要租的是桥车,请输入您要租的车具体型号");
String type=input.next();
Car c=new Car(666,type);
System.out.println("请输入您要租的天数");
int num=input.nextInt();
System.out.print("您要租的是:"+type+" "+"车牌号是"+c.number+" ");
c.calcRent(num);
break;
}else if(choose==2){
System.out.println("您要租的是客车,请问您需要多少座位的客车?");
int number=input.nextInt();
Bus b=new Bus(888,number);
System.out.println("请输入您要租的天数");
int day=input.nextInt();
System.out.print("您要租的是:"+number+"座客车"+" "+"车牌号是"+b.number+" ");
b.calcRent(day);
break;
}else{
System.out.println("输入错误,请重新输入");
}
}
}
}
End.
July31th.2020 homework
练习21
要求:
1.国家 父类
2.中国 说汉语
3.美国 说英语
4.人类 说话 参数 是多态
package july31th.demo2;
public class Nation {
public void speak(){
System.out.println("地球人会说话!");
}
}
class China extends Nation{
@Override
public void speak() {
// TODO Auto-generated method stub
System.out.println("中国人说汉语");
}
}
class American extends Nation{
@Override
public void speak() {
// TODO Auto-generated method stub
System.out.println("美国人说英语");
}
}
class Person{
public void speak(Nation s){
s.speak();
}
}
package july31th.demo2;
public class NationTest {
public static void main(String[] args) {
Person p=new Person();
China c=new China();
American a=new American();
p.speak(c);
//p.speak(a);
}
}
练习22
要求:
1.果树
2.苹果树
3.香蕉树
4.超市 参数 字符串 返回类型多态 相应的水果
package july31th.demo3;
public class SuperMarket {
public Tree getTree(String s){
Tree n=null;
if(s.equals("苹果")){
n=new AppleTree();
}else if(s.equals("香蕉")){
n=new BananaTree();
}
return n;
}
}
class Tree{
public void show(){
System.out.println("树木");
}
}
class AppleTree extends Tree{
@Override
public void show() {
// TODO Auto-generated method stub
System.out.println("苹果");
}
}
class BananaTree extends Tree{
@Override
public void show() {
// TODO Auto-generated method stub
System.out.println("香蕉");
}
}
package july31th.demo3;
public class SuperMarketTest {
public static void main(String[] args) {
SuperMarket market=new SuperMarket();
String s="苹果";
Tree n=market.getTree(s);
n.show();
}
}
End.
Auguest3rd.2020 homework
练习23
1 汽车父类(属性:汽车名称,车牌;方法:显示所有信息)
2 租金接口 (计算租金方法 返回double 方法名(int day租借天数))
3 客车类 (自己属性座位数) 继承汽车 实现租金(重写租金方法 方法中座位数_day) 1 day=500 * 座位数
4 货车类 (自己属性载重量) 继承汽车 实现租金(重写租金方法 方法中载重量_day) 1 day=1000* 多少吨
5 测试类 测试两种车
package august3rd.demo1;
public class Car {
String brand;
String num;
public void info() {
System.out.println("品牌是:" + brand + "车牌是:" + num);
}
}
package august3rd.demo1;
public interface Rental {
double getMoney(int day);
}
package august3rd.demo1;
public class Bus extends Car implements Rental {
int seatNum;
@Override
public double getMoney(int day) {
// TODO Auto-generated method stub
double money=day*500;
return money;
}
}
package august3rd.demo1;
public class Truckload extends Car implements Rental {
int weight;
@Override
public double getMoney(int day) {
// TODO Auto-generated method stub
double money=day*1000;
return money;
}
}
package august3rd.demo1;
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
Scanner input=new Scanner(System.in);
Bus test=new Bus();
System.out.println("请输入所需客车的座位数");
test.seatNum=input.nextInt();
System.out.println("请输入租车天数");
int num=input.nextInt();
System.out.println(test.getMoney(num)+"元");
System.out.println("******************");
Truckload t=new Truckload();
System.out.println("请输入所需货车的载重");
t.weight=input.nextInt();
System.out.println("请输入租车天数");
int day=input.nextInt();
System.out.println(t.getMoney(day)+"元");
}
}
练习24
package august3rd.demo2;
public class HandSet {
String brand;
String type;
public void sendInfo() {
System.out.println("可以发短信");
}
public void call() {
System.out.println("可以打电话");
}
public void info() {
System.out.println("手机品牌是:" + brand + "型号是:" + type);
}
}
package august3rd.demo2;
public interface PlayWiring {
void paly(String content);
}
package august3rd.demo2;
public interface TakePhoto {
void takePhoto();
}
package august3rd.demo2;
public interface NetWork {
void net();
}
package august3rd.demo2;
public class CommonHandset extends HandSet implements PlayWiring {
@Override
public void paly(String content) {
// TODO Auto-generated method stub
System.out.println("可以播放" + content);
}
}
package august3rd.demo2;
public class SmartPhone extends HandSet implements NetWork, TakePhoto,
PlayWiring {
@Override
public void paly(String content) {
System.out.println("可以播放" + content);
}
@Override
public void takePhoto() {
// TODO Auto-generated method stub
System.out.println("可以照像");
}
@Override
public void net() {
// TODO Auto-generated method stub
System.out.println("可以上网");
}
}
package august3rd.demo2;
public class Test {
public static void main(String[] args) {
CommonHandset test=new CommonHandset();
System.out.println("功能机");
test.brand="Nokia";
test.type="5230";
test.info();
test.paly("音乐");
test.sendInfo();
test.call();
System.out.println("**************************");
System.out.println("智能机");
SmartPhone smart=new SmartPhone();
smart.brand="Apple";
smart.type="iphone8 Plus";
smart.info();
smart.net();
smart.paly("视频");
smart.takePhoto();
smart.sendInfo();
smart.call();
}
}
End.
August4th.2020 homework
练习25
package august4th.demo1;
import java.util.InputMismatchException;
import java.util.Scanner;
import org.apache.log4j.Logger;
class Subject {
int firstNum;
int secondNum;
public void getNum(int num) throws Exception {
if (num == 0) {
throw new Exception("出现错误,被除数和除数必须是整数,除数不能为零。");
}
}
}
public class SubjectTest {
public static Logger log = Logger.getLogger("");
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
Subject test = new Subject();
System.out.print("请输入被除数:");
test.firstNum = input.nextInt();
log.debug("输入被除数:"+test.firstNum);
System.out.print("请输入除数:");
test.secondNum = input.nextInt();
log.debug("输入除数:"+test.secondNum);
if(test.secondNum==0){
log.error("/ by zero");
}
try {
test.getNum(test.secondNum);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(test.firstNum / test.secondNum);
log.debug("输出运算结果:"+test.firstNum +"/"+ test.secondNum+"="+(test.firstNum / test.secondNum));
}
}
练习26
package august4th.demo2;
public class Animal {
private String name;
private int legNum;
public Animal(String name, int legNum) {
this.name = name;
this.legNum = legNum;
}
public String getName() {
return name;
}
public int getLegNum() {
return legNum;
}
public void shout() {
System.out.println("动物会叫!");
}
}
class Cat extends Animal {
public Cat(String name, int legNum) {
super(name, legNum);
// TODO Auto-generated constructor stub
}
@Override
public void shout() {
// TODO Auto-generated method stub
System.out.println("猫咪喵喵叫!");
}
}
class Duck extends Animal {
public Duck(String name, int legNum) {
super(name, legNum);
// TODO Auto-generated constructor stub
}
@Override
public void shout() {
// TODO Auto-generated method stub
System.out.println("鸭子嘎嘎叫!");
}
}
package august4th.demo2;
public class AnimalTest {
public static void main(String[] args) {
Animal cat=new Cat("coco", 4);
System.out.print("昵称:"+cat.getName()+"有");
System.out.println(cat.getLegNum()+"条腿");
cat.shout();
System.out.println("**********************");
Animal duck=new Duck("二丫",2);
System.out.print("昵称:"+duck.getName()+"有");
System.out.println(duck.getLegNum()+"条腿");
duck.shout();
}
}
练习27
package august4th.demo3;
public class Animal {
private String name;
public Animal(String name) {
this.name = name;
}
public void shout(){
System.out.println("动物会叫!");
}
}
class Cat extends Animal implements Terrestrial {
private int legNum;
public Cat(String name,int legNum) {
super(name);
this.legNum=legNum;
// TODO Auto-generated constructor stub
}
@Override
public void shout() {
// TODO Auto-generated method stub
System.out.println("猫咪喵喵叫!");
}
@Override
public int getLegNum() {
// TODO Auto-generated method stub
return this.legNum;
}
}
class Duck extends Animal implements Terrestrial{
private int legNum;
public Duck(String name,int legNum) {
super(name);
this.legNum=legNum;
// TODO Auto-generated constructor stub
}
@Override
public void shout() {
// TODO Auto-generated method stub
System.out.println("鸭子嘎嘎叫");
}
@Override
public int getLegNum() {
// TODO Auto-generated method stub
return this.legNum;
}
}
class Dolphin extends Animal{
public Dolphin(String name) {
super(name);
// TODO Auto-generated constructor stub
}
@Override
public void shout() {
// TODO Auto-generated method stub
System.out.println("海豚音");
}
}
package august4th.demo3;
public interface Terrestrial {
int getLegNum();
}
package august4th.demo3;
public class AnimalTest {
public static void main(String[] args) {
Cat cat=new Cat("皮卡丘",4);
System.out.println(cat.getLegNum());
cat.shout();
Duck duck=new Duck("唐老鸭", 2);
System.out.println(duck.getLegNum());
duck.shout();
Dolphin d=new Dolphin("TT");
d.shout();
}
}
End.
August7th.2020 homework
练习28
新建一个List 对象 ,学生类姓名和分数属性 ,新建5个对象,显示所有学生信息。
package august7th;
import java.util.ArrayList;
import java.util.List;
public class StudentTest {
public static void main(String[] args) {
List list=new ArrayList();
Student stu=new Student("小明",78);
Student stu2=new Student("小花",90);
Student stu3=new Student("小刚",23);
Student stu4=new Student("小东",50);
Student stu5=new Student("小花",44);
list.add(stu);
list.add(stu2);
list.add(stu3);
list.add(stu4);
list.add(stu5);
// for(Object o:list){ for each遍历
// Student student=(Student)o;
// student.info();
// }
for(int i=0;i<list.size();i++){
Student student=(Student) list.get(i);
student.info();
}
}
}
class Student{
String name;
int scores;
public Student(String name,int scores){
this.name=name;
this.scores=scores;
}
public void info(){
System.out.println("姓名:"+name+" "+"年龄:"+scores);
}
}
End
August11th.2020 homework
练习29
第一个类 两个构造方法 无参数和两个参数 set/get
package august11th;
import java.util.ArrayList;
import java.util.List;
public class PenguinTest {
public static void main(String[] args) {
List list=new ArrayList();
Penguin p1=new Penguin("东东 ,健康度:",98);
Penguin p2=new Penguin("南南 ,健康度:",96);
Penguin p3=new Penguin("西西 ,健康度:",99);
Penguin p4=new Penguin("北北, 健康度:",95);
list.add(p1);
list.add(p2);
list.add(p3);
list.add(p4);
System.out.println("一共"+list.size()+"只企鹅");
for(Object o:list){
Penguin p=(Penguin)o;
System.out.println(p);
}
System.out.println("删除第一个企鹅的信息
");
list.remove(0);
for(Object o:list){
Penguin p=(Penguin)o;
System.out.println(p);
}
System.out.println("删除西西的信息
");
list.remove(p3);
for(Object o:list){
Penguin p=(Penguin)o;
System.out.println(p);
}
}
}
class Penguin {
private String name = "大白";
int health = 100;
private int love = 0;
String sex = "Q仔";
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getLove() {
return love;
}
public void setLove(int love) {
this.love = love;
}
public Penguin(String name, int love) {
super();
this.name = name;
this.love = love;
}
public Penguin() {
super();
}
@Override
public String toString() {
// TODO Auto-generated method stub
return name+love;
}
}
练习30
package august11th;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
public class DogTest {
public static void main(String[] args) {
// Dog ou=new Dog("欧欧","雪纳瑞");
// Dog ya=new Dog("亚亚","拉布拉多");
// Dog mei=new Dog("美美","柯基");
// Dog fei=new Dog("菲菲","柴犬");
Map<String,String> dog=new HashMap<String,String>();
dog.put("欧欧","雪纳瑞"); //dog.put(key,value)
dog.put("亚亚","拉布拉多");
dog.put("美美","柯基");
dog.put("菲菲","柴犬");
if(dog.containsKey("欧欧")){ //判断是否有key"欧欧"
Dog d=new Dog("欧欧","拉布拉多");
System.out.println(d.getName()+" "+d.getStrain());
}else{
Set set=dog.keySet();
for(Object o:set){
System.out.println(set);
}
}
}
}
class Dog {
private String name;
private int health;
private int love;
private String strain;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getHealth() {
return health;
}
public void setHealth(int health) {
this.health = health;
}
public int getLove() {
return love;
}
public void setLove(int love) {
this.love = love;
}
public String getStrain() {
return strain;
}
public void setStrain(String strain) {
this.strain = strain;
}
public Dog(String name, String strain) {
super();
this.name = name;
this.strain = strain;
}
public Dog(){
}
}
练习31
package august11th;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
public class RabbitTest {
public static void main(String[] args) {
Rabbit r1 = new Rabbit("欧欧", "公");
Rabbit r2 = new Rabbit("亚亚", "母");
Rabbit r3 = new Rabbit("美美", "公");
Rabbit r4 = new Rabbit("菲菲", "母");
Map<String, String> rabbit = new HashMap<String, String>();
rabbit.put(r1.getName(), r1.getSex()); // dog.put(key,value)
rabbit.put(r2.getName(), r2.getSex());
rabbit.put(r3.getName(), r3.getSex());
rabbit.put(r4.getName(), r4.getSex());
for(String n:rabbit.keySet()){
System.out.println(n);
}
System.out.println("*****************");
Iterator it = rabbit.keySet().iterator();
while (it.hasNext()) {
Object s = it.next();
System.out.println(s);
}
}
}
class Rabbit {
private String name = "大白";
int health = 100;
private int love = 0;
String sex = "公";
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public Rabbit(String name, String sex) {
super();
this.name = name;
this.sex = sex;
}
public Rabbit() {
super();
}
@Override
public String toString() {
// TODO Auto-generated method stub
return name + love;
}
}
End
August12th.2020 homework
练习32
package august12th;
import java.util.Scanner;
public class Subject {
public static void main(String[] args) {
for (;;) {
Scanner input = new Scanner(System.in);
System.out.print("请输入用户名:");
input.next();
System.out.print("请输入密码:");
int l = input.nextInt();
Way w = new Way();
w.getLength(l);
Integer it=l;
String s=it.toString();
if(s.length()==6){
break;
}
}
}
}
class Way {
public void getLength(int l) {
Integer it = l;
String s = it.toString();
s.length();
if (s.length() < 6) {
System.out.println("密码长度不小于6位!请重新输入!");
} else if (s.length() > 6) {
System.out.println("密码长度不能大于6位!请重新输入!");
} else {
System.out.println("注册成功!");
}
}
}
练习33
package august12th;
import java.util.Scanner;
public class Subject2 {
public static void main(String[] args) {
for (;;) {
Scanner input = new Scanner(System.in);
System.out.print("请输入用户名:");
String s = input.next();
System.out.print("请输入密码:");
int n = input.nextInt();
if (s.equalsIgnoreCase("tom")) {
System.out.println("用户名正确,登录成功!");
break;
} else {
System.out.println("用户名错误!");
}
}
}
}
练习34
package august12th;
import java.util.Scanner;
public class Subject3 {
public static void main(String[] args) {
System.out.println("***欢迎进入注册系统***
");
for (;;) {
Scanner input = new Scanner(System.in);
System.out.print("请输入用户名:");
String s = input.next();
System.out.print("请输入密码:");
int l = input.nextInt();
Until t = new Until();
t.getLength(s, l);
Integer it = l;
String n = it.toString();
if (s.length() >= 3 && n.length() >= 6) {
System.out.print("请再次输入密码:");
int j = input.nextInt();
t.passWord(l, j);
if (l == j) {
break;
}
}
}
}
}
class Until {
public void getLength(String s, int l) {
Integer it = l;
String n = it.toString();
if (s.length() < 3 || n.length() < 6) {
System.out.println("用户名长度不能小于3,密码长度不能小于6");
}
}
public void passWord(int i, int j) {
if (i == j) {
System.out.println("注册册成功,请牢记用户名和密码");
} else {
System.out.println("两次输入的密码不相同,请重新输入!");
}
}
}
练习35
package august12th;
import java.util.Scanner;
public class Subject4 {
public static void main(String[] args) {
for (;;) {
Scanner input = new Scanner(System.in);
System.out.println("---欢迎进入作业提交系统---");
System.out.print("请输入Java文件名:");
String s = input.next();
System.out.print("请输入邮箱:");
String n = input.next();
Way2 test = new Way2();
test.getInfo(s, n);
int n1 = n.indexOf('.');
int n2 = n.indexOf('@');
if (s.endsWith(".java") && n1 > n2) {
break;
} else {
System.out.println("E-mail无效。
作业提交失败!");
}
}
}
}
class Way2 {
public void getInfo(String s, String t) {
int n1 = t.indexOf('.');
int n2 = t.indexOf('@');
if (s.endsWith(".java") && n1 > n2) {
System.out.println("作业提交成功!");
}
}
}
练习36
package august12th;
public class Subject5 {
public static void main(String[] args) {
String word="Hello, ";
word=word.trim(); //删除前导和尾随空格
String s=word.concat("小鱼儿!"); //将指定的字符串连接到该字符串的末尾。
int index1=s.indexOf(',');
int index2=s.indexOf("!");
//返回一个字符串,该字符串是此字符串的子字符串。 子字符串以指定索引处的字符开头,并扩展到该字符串的末尾。
System.out.println(s.substring(0, s.length()));
}
}
练习37
package august12th;
public class Subject6 {
public static void main(String[] args) {
System.out.println("***原歌词格式***");
System.out.println("长亭外 古道边 芳草碧连天 晚风拂柳笛声残 夕阳山外山
");
System.out.println("***拆分后词模式***");
String s="长亭外 古道边 芳草碧连天 晚风拂柳笛声残 夕阳山外山";
/*此方法返回的数组包含此字符串的每个子字符串,该字符串由与给定表达式匹配的另一个子字符串终止,
*或由字符串结尾终止。 数组中的子字符串按照它们在此字符串中的顺序排列。
* 如果表达式与输入的任何部分不匹配,则生成的数组只有一个元素,即这个字符串。 */
String[]arr=s.split(" ", 5);
for(String i:arr){
System.out.println(i);
}
}
}
练习38
package august12th;
import java.util.Scanner;
public class Subject7 {
public static void main(String[] args) {
Scanner input=new Scanner(System.in);
System.out.println("请输入一串数字");
String num=input.next();
/*由start和end的参数s的顺序按顺序插入到指定目的地偏移量的该序列中,向上移动原始位于该位置的任何字符。
* 该序列的长度增加了end - start 。*/
StringBuffer s=new StringBuffer(num);
for(int i=s.length()-3;i>0;i=i-3){
s.insert(i, ',');
}
System.out.println(s);
}
}
Ps:开始学java高级语法了,渐渐的感觉有些吃力,不过还好,9月1估计能开线下课了,加油吧,这次作业全用循环写了,有时候逻辑做了效果出不来找半天,不过解决了以后会有很大的成功感!
End
August15th.2020 homework
一周没更新了,今天补下作业。
练习39
定义一个方法 参数是字符串 返回int
要求 参数是生日 类型为 1900-1-1 返回 年龄
package august15th;
/**
* 输入字符串生日,获取当前年龄
* */
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;
public class Subject {
public static void main(String[] args) throws Exception {
Scanner input=new Scanner(System.in);
System.out.println("请输入你的生日:(格式XXXX-X-X)");
String s=input.next();
SimpleDateFormat f=new SimpleDateFormat("yyyy-MM-dd");
Date d=f.parse(s);
long date=d.getTime();
Date time=new Date();
long date1=time.getTime();
System.out.println((date1-date)/1000/60/60/24/365);
}
}
练习40
定义一个方法 参数是1 double 2 int 返回double
要求 参数1 是double 2是整数要求保留的小数点后几位 ,方法中对参数1进行格式化,在转换成double类型返回结果
package august15th;
import java.text.DecimalFormat;
import java.util.Scanner;
public class Subject2 {
public static void main(String[] args) {
double dec;
Scanner input = new Scanner(System.in);
System.out.println("请输入一个保留小数点后5位的数");
double n = input.nextDouble();
System.out.println("请输入想要保留小数点后几位?(可选1、2、3、4)");
int n1 = input.nextInt();
switch(n1){
case 1:
System.out.println(new DecimalFormat("#.#").format(n));
break;
case 2:
System.out.println(new DecimalFormat("#.##").format(n));
break;
case 3:
System.out.println(new DecimalFormat("#.###").format(n));
break;
case 4:
System.out.println(new DecimalFormat("#.####").format(n));
}
}
}
练习41
定义一个方法 参数是File类对象 ,返回字符串
要求 参数的一个文件,方法中 得到 文件的最后一次修改日期,格式化格式为 2020-1-1 11:20:33 把格式化后的字符串 返回
package august15th;
import java.io.File;
import java.io.IOException;
import java.text.Format;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
public class Subject3 {
public static void main(String[] args) throws IOException {
File f=new File("D://a.txt");
long time=f.lastModified();
Date d=new Date(time);
Format SimpleDateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Calendar cal=Calendar.getInstance();
cal.setTimeInMillis(f.lastModified());
String time1=SimpleDateFormat.format(cal.getTime());
System.out.println(time1);
}
}
这道题不是很懂,网上找了下类似的参考一下!
练习42
定义一个方法 参数是File类对象 ,返回字符串
要求 参数的一个文件,方法中 得到 文件的大小,要求返回文件的大小 可以自动转换为 byte kb mb gb 返回字符串
package august15th;
import java.io.File;
public class Subject4 {
public static void main(String[] args) {
File f=new File("D:/A/a.txt/w.txt");
long a=f.length();
System.out.println(a);
System.out.println((f.length()/1024.0/1024.0));
}
}
Ps:感觉作页越来越难了,有什么不对的欢迎大家指出来!!
END
August18th.2020 homework
练习43
封装一个方法可以复制文件
返回类型 boolean 复制是否成功
参数源文件,目标目录(这题做的有点问题!!)
package august18th;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class Subject {
public static void main(String[] args) {
File f=null;
File o=null;
FileOutputStream out=null;
FileInputStream in=null;
try{
f=new File("E:/Java76/Java/io/Test/a.txt");
o=new File("E:/Java76/Java/io/java/b.txt");
in=new FileInputStream(f);
out=new FileOutputStream(o);
byte[]b=new byte[1024];
for(int count=0;(count=in.read(b))!=-1;){
out.write(b,0,count);
out.flush();
}
System.out.println("复制成功");
}catch(Exception e){
e.printStackTrace();
System.out.println("复制失败");
}finally{
try{
in.close();
}catch(IOException e){
e.printStackTrace();
}
} try{
out.close();
}catch(IOException e){
e.printStackTrace();
}
}
}
练习44
封装一个方法 可以把指定的内容输出到指定的目录文件
返回类型 boolean
参数(内容,目标文件)
package august18th;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
public class Subject2 {
public static void main(String[] args) throws Exception {
File f=new File("E:"+File.separator+"java76/a.txt"); //目标文件输入流
InputStream is=new FileInputStream(f);
byte[]c=new byte[1024];
//String s=" asdf";
// for(int count=0;(count=is.read(c))!=-1;){
// s+=new String(c,0,count);
// }
// System.out.println(s);
is.close();
File f1=new File("E:"+File.separator+"java76/a.txt"); //目标文件输出流
OutputStream out=new FileOutputStream(f1,true);
String t="你好";
out.write(t.getBytes());
out.write(t.getBytes());
out.close();
}
}
练习45
封装一个方法 读取一个文本文件
返回类型 Sting 读取的文件内容
参数(源文件)
package august18th;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.Reader;
import java.io.Writer;
public class Subject3 {
public static void main(String[] args) throws Exception { //字符输入输出流读写!
File f=new File("D:"+File.separator+"a.txt");
Reader read=new FileReader(f);
char[]c=new char[3];
String s=" ";
for(int count=0;(count=read.read(c))!=-1;){
s+=new String(c,0,count);
}
System.out.println(s);
File f1=new File("D:"+File.separator+"a.txt");
Writer w=new FileWriter(f1);
w.write("大保健");
w.flush();
w.close();
}
}
PS:今天是我27岁生日,没有惊喜,没有意外,只有老爸晚上给我发了个生日红包,果然还是只有家人最关心自己,加油吧,定了场livehouse的票,第一次去看,真的很喜欢Hip-hop,加油吧,心态放端正就好!!
End
August19th.2020 homework
练习46
使用 BufferedReader 读取一个.txt 文件的内容
package august19th;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.Reader;
public class Subject {
public static void main(String[] args) throws Exception {
File f=new File("a.txt");
//字符输入流
Reader r=new FileReader(f);
BufferedReader br=new BufferedReader(r,1024*1024);
// String s=br.readLine();
// System.out.println(s);
// String s1=br.readLine();
// System.out.println(s1);
String s=" ";
for(String sline=" ";(sline=br.readLine())!=null;){
s+= sline+"
";
}
System.out.println(s);
r.close();
br.close();
}
}
练习47
ObjectInputStream, ObjectOutputStream ,实现对象的序列号和反序列化习47
package august19th;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.io.Serializable;
public class Subject3 {
public static void main(String[] args) throws Exception {
// Users u = new Users(1, "333");
File f = new File("a.txt");
// 序列话操作
// OutputStream w = new FileOutputStream(f);
// ObjectOutputStream out = new ObjectOutputStream(w);
// out.writeObject(u);
// out.flush();
//反序列化操作
InputStream o=new FileInputStream(f);
ObjectInputStream in=new ObjectInputStream(o);
Users u=(Users)in.readObject();
System.out.println(u.getAge()+" "+u.getName());
}
}
class Users implements Serializable {
private int age;
private String name;
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Users() {
super();
}
public Users(int age, String name) {
super();
this.age = age;
this.name = name;
}
}
练习48
使用继承和实现接口的两种方法,开启子线程复制 一个文件文章来源:https://www.toymoban.com/news/detail-759768.html
package august19th;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class Subject2 extends Thread {
public void run() {
File f=null;
File o=null;
FileOutputStream out=null;
FileInputStream in=null;
try{
f=new File("E:/Java76/Java/io/Test/a.txt");
o=new File("E:/Java76/Java/io/java/b.txt");
in=new FileInputStream(f);
out=new FileOutputStream(o);
byte[]b=new byte[1024];
for(int count=0;(count=in.read(b))!=-1;){
out.write(b,0,count);
out.flush();
}
System.out.println("复制成功");
}catch(Exception e){
e.printStackTrace();
System.out.println("复制失败");
}finally{
try{
in.close();
}catch(IOException e){
e.printStackTrace();
}
} try{
out.close();
}catch(IOException e){
e.printStackTrace();
}
}
public static void main(String[] args) {
Thread test=new Thread(new Subject2());
test.start();
}
}
class Sub implements Runnable{
@Override
public void run() {
// TODO Auto-generated method stub
File f=null;
File o=null;
FileOutputStream out=null;
FileInputStream in=null;
try{
f=new File("E:/Java76/Java/io/Test/a.txt");
o=new File("E:/Java76/Java/io/java/b.txt");
in=new FileInputStream(f);
out=new FileOutputStream(o);
byte[]b=new byte[1024];
for(int count=0;(count=in.read(b))!=-1;){
out.write(b,0,count);
out.flush();
}
System.out.println("复制成功");
}catch(Exception e){
e.printStackTrace();
System.out.println("复制失败");
}finally{
try{
in.close();
}catch(IOException e){
e.printStackTrace();
}
} try{
out.close();
}catch(IOException e){
e.printStackTrace();
}
}
}
此系列完结,后续我会更新进阶版的编程题,大家一起进步!文章来源地址https://www.toymoban.com/news/detail-759768.html
到了这里,关于Java从入门到熟悉的48道练习题[完结]的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!