import java.util.Scanner;
class Demo5_2{
/*
类
StopWatch
属性 startTime endTime
无参构造函数 初始化startTime
行为 start() 将startTime重设为当前时间
stop() 将endTime设为当前时间
getElapsedTime() 记录过去了多少时间
*/
public static void main(String[] args){
StopWatch s=new StopWatch();
long start=s.start();
System.out.println(start);
if(s.ifStop()){
long end=s.stop();
System.out.println(end);
System.out.println(s.getElapsedTime(start,end));
}
}
}
class StopWatch{
long startTime;
long endTime;
public StopWatch(){
this.startTime=System.currentTimeMillis();
}
public long start(){
this.startTime=System.currentTimeMillis();
return startTime;
}
public long stop(){
this.endTime=System.currentTimeMillis();
return endTime;
}
public long getElapsedTime(long startTime,long endTime){
return (endTime-startTime);
}
public boolean ifStop(){
Scanner scanner=new Scanner(System.in);
if (scanner.nextBoolean()){
return true;
}else{
return false;
}
}
public long getStartTime(){
return this.startTime;
}
public long getEndTime(){
return this.endTime;
}
}
import java.util.Scanner;
class Demo5_3{
public static void main(String[] args){
Fan f=new Fan();
Scanner scanner=new Scanner(System.in);
while(true){
int i;
boolean flag=false;
System.out.println("请输入0(关)或1(开):");
i=scanner.nextInt();
if(i==1){
flag=true;
f.setOn(flag);
System.out.println(f.toString(f.isOn()));
break;
}else if(i==0){
System.out.println(f.toString(f.isOn()));
break;
}else {
System.out.println("请重新输入:");
}
}
}
}
class Fan{
final static int SLOW=1;
final static int MEDIUM=2;
final static int FAST=3;
private int speed=SLOW;
private boolean on=false;
private double radius=5;
private String color="blue";
public Fan(){
}
public void setSpeed(int speed){
this.speed=speed;
}
public void setOn(boolean on){
this.on=on;
}
public void setRadius(int radius){
this.radius=radius;
}
public void setColor(String color){
this.color=color;
}
public int getSpeed(){
return this.speed;
}
public boolean isOn(){
return this.on;
}
public double getRadius(){
return this.radius;
}
public String getColor(){
return this.color;
}
public String toString(boolean on){
String s;
if(on){
s=this.speed+":"+this.color+":"+this.radius;
return s;
}else{
s="Fan is off:"+this.color+":"+this.radius;
return s;
}
}
}
import java.util.Scanner;
class Demo5_4{
public static void main(String[] args){
Scanner scanner=new Scanner(System.in);
System.out.print("请输入a,b,c:");
QuadraticEquation q=new QuadraticEquation(scanner.nextDouble(),scanner.nextDouble(),scanner.nextDouble());
if(q.getDiscriminant()>0){
double x1=q.getRoot1(q.getDiscriminant());
double x2=q.getRoot2(q.getDiscriminant());
System.out.println("x1="+x1);
System.out.println("x2="+x2);
}else if(q.getDiscriminant()==0){
double x=q.getRoot2(q.getDiscriminant());
System.out.println("x="+x);
}else{
System.out.println("The equation has no roots.");
}
}
}
class QuadraticEquation{
private double a;
private double b;
private double c;
public QuadraticEquation(){
this(0,0,0);
}
public QuadraticEquation(double a,double b,double c){
this.a=a;
this.b=b;
this.c=c;
}
public double getA(){
return this.a;
}
public double getB(){
return this.b;
}
public double getC(){
return this.c;
}
public double getDiscriminant(){
double delt=Math.pow(getB(),2)-4*getA()*getC();
return delt;
}
public double getRoot1(double delt){
double x1=(-getB()+getDiscriminant())/(2*getA());
return x1;
}
public double getRoot2(double delt){
double x2=(-getB()-getDiscriminant())/(2*getA());
return x2;
}
}
import java.util.Scanner;
class Demo5_5{
public static void main(String[] args){
Scanner scanner=new Scanner(System.in);
System.out.print("Enter row-col:");
double[][] arr=new double[scanner.nextInt()][scanner.nextInt()];
System.out.println("请输入矩阵:");
for(int i=0;il.maxValue){
l.row=i;
l.col=j;
l.maxValue=a[i][j];
}
}
}
System.out.print("The location of the largest is "+l.maxValue+" at "+"("+l.row+","+l.col+")");
}
}
class Demo5_6{
public static void main(String[] args){
Circle c1=new Circle(2.0,2.0,5.5);
Circle c2=new Circle();
System.out.println(c1.getArea());
System.out.println(c2.getArea());
System.out.println(c1.getPerimeter());
System.out.println(c1.contains (3,3));
System.out.println(c1.contains(new Circle(4,5,10.5)));
System.out.println(c1.overlaps(new Circle(3,5,2.3)));
}
}
class Circle{
private Pointer center;
private double radius;
public Circle(){
this(0,0,1);
}
public Circle(double x,double y){
this.center=new Pointer(x,y);
}
public Circle(double x,double y,double radius){
center=new Pointer(x,y);
this.radius=radius;
}
public double getArea(){
return Math.PI*Math.pow(this.radius,2);
}
public double getPerimeter(){
return Math.PI*2*this.radius;
}
public double getRadius(){
return this.radius;
}
public boolean contains(double x,double y){
Pointer c=new Pointer(x,y);
double distance=center.getDistance(c);
if(distance>this.radius){
return false;
}else{
return true;
}
}
public boolean contains(Circle other){
double distance=center.getDistance(other.center);
if(distance>Math.abs(this.radius-other.radius)){
return false;
}else{
return true;
}
}
public boolean overlaps(Circle other){
double distance=center.getDistance(other.center);
if(distance(this.radius-other.radius)){
return false;
}else{
return true;
}
}
}
class Pointer{
private double x;
private double y;
public Pointer(){
this(0,0);
}
public Pointer(double x,double y){
this.x=x;
this.y=y;
}
public double getDistance(Pointer c){
double deltX=this.x-c.x;
double deltY=this.y-c.y;
return Math.sqrt(Math.pow(deltX,2)+Math.pow(deltY,2));
}
public double getX(){
return this.x;
}
public double getY(){
return this.y;
}
}
class Demo5_7{
public static void main(String[] args){
MyDate md=new MyDate();
System.out.println(md.getYear());
System.out.println(md.getMonth());
System.out.println(md.getDay());
}
}
class MyDate{
private long year;
private long month;
private long day;
private long days;
private static int[] daysOfYear;
private static int[] runYears={31,29,31,30,31,30,31,31,30,31,30,31};
private static int[] pingYears={31,28,31,30,31,30,31,31,30,31,30,31};
//静态代码块 类加载完毕后直接执行
static{
daysOfYear=new int[50];//1970-1971...
for(int i=1970;i
class Demo5_8{
public static void main(String[] args){
RegularPolygon r=new RegularPolygon(6,4);
RegularPolygon r1=new RegularPolygon(10,4,5.6,7.8);
RegularPolygon r2=new RegularPolygon();
System.out.println(r.getPerimeter());
System.out.println(r.getArea());
System.out.println(r1.getPerimeter());
System.out.println(r1.getArea());
System.out.println(r2.getPerimeter());
System.out.println(r2.getArea());
}
}
class RegularPolygon{
private int n;
private double side;
private Pointer center;
public RegularPolygon(){
this(3,1,0,0);
}
public RegularPolygon(int n,double side){
this(n,side,0,0);
}
public RegularPolygon(int n,double side,double x,double y){
this.n=n;
this.side=side;
this.center=new Pointer(x,y);
}
public int getN(){
return this.n;
}
public double getSide(){
return this.side;
}
public Pointer getCenter(){
return this.center;
}
public double getPerimeter(){
return this.n*this.side;
}
public double getArea(){
return (this.n*Math.pow(this.side,2))/(4*Math.tan(Math.PI/this.n));
}
}
class Pointer{
private double x;
private double y;
public Pointer(){
this(0,0);
}
public Pointer(double x,double y){
this.x=x;
this.y=y;
}
public double getX(){
return this.x;
}
public double getY(){
return this.y;
}
}
8.Arrays工具类
class ArraysDemo{
public static void main(String[] args){
int[] arr=new int[]{5,6,2,3,4,1,8,9,7};
System.out.println(MyArrays.toString(arr));
MyArrays.sort(arr);
System.out.println(MyArrays.toString(arr));
System.out.println(MyArrays.binarySearch(arr,4));
System.out.println(MyArrays.binarySeachUpper(arr,4));
//System.out.println(MyArrays.toString(arr));
}
}
class MyArrays{
public static void sort(int[] a){
if(a==null){
System.out.println("数组为空");
return;
}
if(a.length==0){
System.out.println("数组中没有元素");
return;
}
for(int i=1;i0&&a[j-1]>e;j--){
a[j]=a[j-1];
}
a[j]=e;
}
}
public static int binarySearch(int[] a,int key){
if(a==null){
System.out.println("数组为空");
return -1;
}
if(a.length==0){
System.out.println("数组中没有元素");
return -1;
}
if(isSort(a)){
int min=0;
int max=a.length-1;
int mid=(min+max)/2;
while(a[mid]!=key){
if(a[mid]>key){
max=mid-1;
}else if(a[mid]max){
System.out.println("找不到");
return -1;
}
}
return mid;
}
System.out.println("找不到");
return -1;
}
private static boolean isSort(int[] a){
for(int i=0;ia[i+1]){
return false;
}
}
return true;
}
public static int[] copy(int[] a,int newLength){
int[] b=new int[newLength];
for(int i=0;i0&&b[j-1]>e;j--){
b[j]=b[j-1];
indexs[j]=indexs[j-1];
}
b[j]=e;
indexs[j]=index;
}
int index=binarySearch(b,key);
return b[index];
}
public static String toString(int[] a){
if(a.length==0){
return "["+"]";
}
String s="[";
for(int i=0;i
9.吃鸡
import java.util.Arrays;
class ChiJi{
public static void main(String[] args){
Player p1=new Player("小明");
Player p2=new Player("老王");
Clips clips=new Clips();
for(int i=1;i<=20;i++){
p1.zhuangDan(clips,new Bullent());
}
Gun gun=new Gun();
p1.takeGun(gun);
p1.shangTang(clips);
for(int i=1;i<=30;i++){
p1.kaiQiang(p2);
if(p2.HP<=0){
break;
}
}
System.out.println("死亡");
System.out.println(p2.HP);
}
}
/*
类 玩家 Player
属性 昵称 name
血量 HP
枪 gun
行为 捡枪(枪)
装弹(弹夹,子弹)
上膛(弹夹)
开枪(玩家)
掉血(伤害值)
*/
class Player{
String name;
int HP;
Gun gun;
public Player(String name){
this.name=name;
this.HP=100;
this.gun=null;
}
public void takeGun(Gun gun){
this.gun=gun;
}
public void zhuangDan(Clips clips,Bullent bullent){
clips.loding(bullent);
}
public void shangTang(Clips clips){
gun.takeClips(clips);
}
public void kaiQiang(Player enemy){
gun.shoot(enemy);
}
public void diaoXue(int damage){
HP-=damage;
}
}
/*
类 枪
属性 弹夹 clips
行为 装弹夹=Player上膛(弹夹)
开枪(玩家)
*/
class Gun{
Clips clips;
public void takeClips(Clips clips){
this.clips=clips;
}
public void shoot(Player enemy){
Bullent bullent=clips.out();
if(bullent==null){
System.out.println("放了 一个空枪");
}else{
bullent.hit(enemy);
}
}
}
/*
类 弹夹
属性 子弹 bullent
行为 装弹(子弹)
出弹()
*/
class Clips{
Bullent[] bullents=new Bullent[0];
int maxSize=30;
public void loding(Bullent bullent){
if(bullents.length>=maxSize){
System.out.println("弹夹已满");
}else{
bullents=Arrays.copyOf(bullents,bullents.length+1);
bullents[bullents.length-1]=bullent;
}
}
public Bullent out(){
Bullent bullent;
if(bullents.length>0){
bullent=bullents[bullents.length-1];
bullents=Arrays.copyOf(bullents,bullents.length-1);
return bullent;
}else{
System.out.println("没有子弹了!!!");
return null;
}
}
}
/*
类 子弹
属性 伤害值 damage
行为
命中 hit(玩家)
*/
class Bullent{
int damage=10;
public void hit(Player enemy){
enemy.diaoXue(damage);
}
}
10.接口
class InterfaceDemo{
public static void main(String[] args){
Button bt_login=new Button();
//局部内部类(匿名实现子类的匿名对象)
bt_login.doSomething(new Task(){
public void doWhat(){
System.out.println("登录....");
}
});
}
}
class Button{
public void doSomething(Task task){
task.doWhat();
}
}
interface Task{
public abstract void doWhat();
}
**11.内部类 **
class InsideDemo{
public static void main(String[] args){
A a=new A();
A.B b=a.new B();
b.show();
System.out.println(a.num);
System.out.println(b.num);
}
}
class A{
int num=1;
class B{
int num=2;
public void show(){
System.out.println("B ......");
System.out.println(A.this.num);
}
}
}
12.抽象类
class AbstractDemo{
public static void main(String[] args){
Animal a=new Dog();
a.eat();
Animal b=new Cat();
b.eat();
}
}
abstract class Animal{
public abstract void eat();
}
class Dog extends Animal{
public void eat(){
System.out.println("狗吃骨头");
}
}
class Cat extends Animal{
public void eat(){
System.out.println("猫吃鱼");
}
}
13.代理模式
class DaiLiDemo{
public static void main(String[] args){
Wang w=new Wang();
w.setP("Ma");
w.happy();
}
}
interface BadWoman{
void happy();
}
class Wang implements BadWoman{
BadWoman b;
public void setP(String name){
if(name.equals("Ma")){
b=new Ma();
}else if(name.equals("Pan")){
b=new Pan();
}
}
public void happy(){
b.happy();
}
}
class Ma implements BadWoman{
public void happy(){
System.out.println("Ma happy!");
}
}
class Pan implements BadWoman{
public void happy(){
System.out.println("Pan happy!");
}
}
14.单例模式
class DanLiDemo{
public static void main(String[] args){
A a=A.setA();
A b=A.setA();
System.out.println(a==b);
B b1=B.setB();
B b2=B.setB();
System.out.println(b1==b2);
}
}
//饿汉模式
class A{
public static A a=new A();
public static A setA(){
return a;
}
}
//饱汉模式
class B{
public static B b=null;
public static B setB(){
if(b==null){
b=new B();
}
return b;
}
}
15.静态内部类
class StaticInsideDemo{
public static void main(String[] args){
A a=new A();
System.out.println(a.num);
System.out.println(A.B.num);
new A.B().show();
}
}
class A{
int num=1;
static class B{
static int num=2;
public void show(){
System.out.println("B......");
}
}
}