1.test_1 ( 面向对象 )
package com.jereh4;
public class Test_8_10 {
private String name = "";
private int age = 0 ;
private String sex = "";
private int high = 0 ;
private int weight = 0 ;
public Test_8_10(String name, int age , String sex){
this.name = name;
this.age = age;
this.sex = sex;
}
private static Test_8_10 test = new Test_8_10("小美", 21, "女");
public static Test_8_10 getTest(){
return test;
}
public void setName(String name){
this.name = name;
}
public String getName(){
return name;
}
public void setAge(int age){
this.age = age;
}
public int getAge(){
return age;
}
public void setSex(String sex){
this.sex = sex;
}
public String getSex(){
return sex;
}
public void sleep(){
System.out.println("睡觉!");
}
public void say(){
System.out.println("我叫"+name);
System.out.println("我今年"+age+"岁了");
System.out.println("我的性别是"+sex);
}
}
2.test_2 ( I/O )
package com.jereh4;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FilenameFilter;
import java.io.IOException;
import java.util.regex.Pattern;
public class Demo_8_10 {
public static void main(String[] args){
// boolean b;
//
// b = Pattern.matches("a*b", "aaaab");
//
// Pattern p = Pattern.compile("a*b");
// Matcher m = p.matcher("bbb");
// b = m.matches();
//
// System.out.println(b);
// FilenameFilter f = new DirFilter(".txt");
File file = new File("testBing.txt");
// File[] f = new File();
// String[] list;
// try {
// System.out.println(file.createNewFile());
// } catch (IOException e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// }
System.out.println(file.canWrite());
// System.out.println(file.canRead());
// System.out.println(file.isAbsolute());
// System.out.println(file.getAbsolutePath());
// System.out.println(file.getAbsoluteFile());
// list = file.list();
//Arrays.sort(list);
// for(int i = 0; i < list.length; i++)
// System.out.println(list[i]);
byte[] b = {1,2,3,4,5,6,7,8,9};
try {
FileOutputStream f = new FileOutputStream(file);
try {
f.write(b);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
class DirFilter implements FilenameFilter{
private Pattern pattern;
public DirFilter(String regex){
pattern = pattern.compile(regex);
}
@Override
public boolean accept(File dir, String name) {
// TODO Auto-generated method stub
return pattern.matcher(new File(name).getName()).matches();
}
}
3.test_3 ( I/O )
package competion;
import java.io.File;
import java.io.FilenameFilter;
import java.util.Arrays;
import java.util.regex.Pattern;
public class DirList {
// public static FilenameFilter filter(final String regex){
//
// return new FilenameFilter(){
//
// Pattern pattern = Pattern.compile(regex);
//
// public boolean accept(File dir, String name){
// return pattern.matcher(new File(name).getName()).matches();
// }
//
// };
//
// }
public static void main(final String[] args){
File path = new File(".");
// System.out.print(path.getName());
String[] list = null;
if(args.length == 0)
list = path.list();
// else
// list = path.list(new FilenameFilter(){
// private Pattern pattern = Pattern.compile(args[0]);
//
// public boolean accept(File dir,String name){
// return pattern.matcher(new File(name).getName()).matches();
// }
// });
Arrays.sort(list);
for(int i = 0; i < list.length; i++)
System.out.println(list[i]);
}
}
//class DirFilter implements FilenameFilter{
//
// private Pattern pattern;
//
// public DirFilter(String regex){
// pattern = pattern.compile(regex);
// }
//
// @Override
// public boolean accept(File dir, String name) {
// // TODO Auto-generated method stub
// return pattern.matcher(new File(name).getName()).matches();
// }
//
//}
4.test_4 ( I/O )
package competion;
import java.io.File;
public class MakeDirectories {
private static void usage(){
System.err.println("error");
System.exit(1);
}
private static void fileData(File f){
System.out.println("absoulte:"+f.getAbsolutePath()+
"\n read:"+f.canRead()+
"\n write:"+f.canWrite()+
"\n name"+f.getName()+
"\n parent"+f.getParent()+
"\n path"+f.getPath()+
"\n length"+f.length()+
"\n modified:"+f.lastModified());
if(f.isFile())
System.out.println("is a File");
else if(f.isDirectory())
System.out.println("is a directory");
}
public static void main(String[] args){
if(args.length<1)
usage();
if(args[0].equals("-r")){
if(args.length!=3) usage();
File
old = new File(args[1]),
rname = new File(args[2]);
old.renameTo(rname);
fileData(old);
fileData(rname);
return;
}
int count = 0;
boolean del = false;
if(args[0].equals("-d")){
count++;
del = true;
}
count --;
while(++count<args.length){
File f = new File(args[count]);
if(f.exists()){
System.out.println(f+"exists");
if(del){
System.out.println("deleting..."+f);
f.delete();
}
}else{
if(!del){
f.mkdirs();
System.out.println("created"+f);
}
}
fileData(f);
}
if(args.length == 1 && args[0].equals("MakeDirectoriesTest"));
}
}
- test_5 ( 面向对象——计算器 )
package com.jereh5;
import java.util.Scanner;
public class Calculator {
private double fristNum = 0;
private double secondNum = 0;
public Calculator(double fristNum,double secondNum){
this.fristNum = fristNum;
this.secondNum = secondNum;
}
public double add(){
return fristNum+secondNum;
}
public double minus(){
return fristNum-secondNum;
}
public double mult(){
return fristNum*secondNum;
}
public double except(){
return fristNum/secondNum;
}
public static void main(String[] args){
Scanner scn = new Scanner(System.in);
System.out.println("请输入第一个数:");
double first = scn.nextDouble();
System.out.println("请输入第二个数:");
double second = scn.nextDouble();
Calculator c = new Calculator(first,second);
System.out.println("请选择你要进行的运算:");
int num = scn.nextInt();
double result = num > 5? c.minus() : (num>10? (num>100? c.except() : c.mult()) : c.add()) ;
System.out.println("计算结果为:"+result);
}
}
- test_6 ( 面向对象 )
package com.jereh5;
public class Coordinate {
private int x;
private int y;
public Coordinate(int x,int y){
this.x = x;
this.y = y;
}
public int getX(){
return x;
}
public int getY(){
return y;
}
public static void main(String[] args){
Coordinate coo = new Coordinate(23,54);
System.out.println("x坐标"+coo.getX()+"\ny坐标"+coo.getY());
}
}
- test_7 (面向对象——模拟提款机)
package com.jereh5;
import java.util.Scanner;
public class User {
private String name = "";
private int pwd = 1111;
private boolean boolInput = false;
private boolean boolMoney = false;
public User(String name, int pwd){
this.name = name;
this.pwd = pwd ;
}
public boolean jadgeInput(){
if(pwd==1111){
boolInput = true;
}else{
boolInput = false;
}
return boolInput;
}
public boolean jadgeMoney(int num){
if(num%100==0){
boolMoney = true;
}else{
boolMoney = false;
}
return boolMoney;
}
public static void main(String[] args){
Scanner scn = new Scanner(System.in);
for(int i = 1; i < 4; i++){
System.out.println("请输入用户名:");
String inputUser = scn.next();
System.out.println("请输入密 码 :");
int inputPwd = scn.nextInt();
User user = new User(inputUser,inputPwd);
if(user.jadgeInput()){
while(true){
System.out.println("请输入要取的金额:");
int inputNum = scn.nextInt();
if(user.jadgeMoney(inputNum)){
System.out.println(user.name+"先生,您取款的金额为:"+inputNum+"元");
break;
}else{
System.out.println("金额输入有误!");
}
}
}else{
System.out.println("密码错误!");
}
}
System.out.println("SB,这不是你的卡!");
}
}
- test_8 (面向对象——门票)
package com.jereh5;
import java.util.Scanner;
public class Visitor {
private String name = "";
private int age = 0 ;
private int ticket = 100;
public Visitor(String name , int age){
this.name = name;
this.age = age;
}
public void showPrice(){
if(age<=10){
System.out.println(name+"的年龄为:"+age+",所以票价为:免费");
}else if(age<20){
System.out.println(name+"的年龄为:"+age+",所以票价为:"+ticket*0.8+"元");
}else{
System.out.println(name+"的年龄为:"+age+",所以票价为:"+ticket+"元");
}
}
public static void main(String[] args){
Scanner scn = new Scanner(System.in);
System.out.println("大家注意啦:儿童免费,青年八折,中年全票!");
System.out.print("请输入您的姓名:");
String inputName = scn.next();
System.out.print("请输入您的年龄:");
int inputAge = scn.nextInt();
Visitor vi = new Visitor(inputName,inputAge);
vi.showPrice();
}
}
- test_9 (面向对象——求矩形面积和周长)
package com.jereh5;
public class Rectangle {
private int length = 1,
width = 1;
public void setLength(int length){
this.length = length;
}
public int getLength(){
return length;
}
public void setWidth(int width){
this.width = width;
}
public int getWidth(){
return width;
}
public int perimeter(){
return 2*(length+width);
}
public int area(){
return length*width;
}
public static void main(String[] args){
Rectangle rec = new Rectangle();
rec.setLength(12);
rec.setWidth(8);
System.out.println("周长为:"+rec.perimeter());
System.out.println("面积为:"+rec.area());
}
}
- test_10 (面向对象)
package com.jereh5;
public class Student {
private String name = "";
private String classes = "";
private double sorce = 0 ;
public void setName(String name){
this.name = name;
}
public String getName(){
return name;
}
public void setClasses(String classes){
this.classes = classes;
}
public String getClasses(){
return classes;
}
public void setSorce(double sorce){
this.sorce = sorce;
}
public double getSorce(){
return sorce;
}
public void getResult(){
System.out.println(classes+name+",你的成绩是:"+sorce);
}
public void getResult(double sorce){
System.out.println(classes+name+",你的成绩是:"+sorce);
}
public static void main(String[] args){
Student student = new Student();
student.setName("小名");
student.setSorce(90);
student.setClasses("大班");
student.getResult();
}
}