类和对象练习

1.
public class Point {
    int x;
    int y;
    double getDistance(){
        double distance=Math.sqrt(x*x+y*y);
        return distance;
    }
    public static  void main(String[] args){
        Point p = new Point();
        p.x=3;
        p.y=4;
        System.out.print(p.getDistance());
    }
}


2.

 public class FindWrong {

    public static void main(String[] args) { int i=1, j; //j没有初始化
        float f1 = 0.1, f2 = 123;  //0.1后面应该加f 或者 前面加(float)
        final double PI = 3.1415926;
        char class = 'b'//class 是关键字
        i=j+5;
        f1=f2+2;
        PI=PI*2; //final不能改变
        z ='c';
    }}

3.

public class Point {
    int x;
    int y;
    public Point(int x1,int y1){
        x=x1;
        y=y1;
    }
    double getDistance(){
        double distance=Math.sqrt(x*x+y*y);
        return distance;
    }
    public static  void main(String[] args){
    Point p =new Point(3,4);
    System.out.println(p.getDistance());
    }
}


4.
import java.awt.*;
import java.sql.Statement;

public class Line {
    Point startPoint;
    Point endPoint;
    public Line(Point p1,Point p2){
        startPoint = p1;
        endPoint = p2;
    }
    public Line(int x1,int y1,int x2,int y2){
       startPoint = new Point(x1,y1);
       endPoint = new Point(x2,y2);
    }
    double getLength(){
        int x = startPoint.x-endPoint.x;
        int y = startPoint.y-endPoint.y;
        double length=Math.sqrt(x*x+y*y);
        return length;
    }
    public static void main(String[] args){
        Point p1 = new Point(1,1);
        Point p2 = new Point(2,2);
        Line line1 = new Line(p1,p2);
        System.out.print(line1.getLength());
    }

}


5.

public class Student {
    int age;
    String name;
    static int count=0;
    public  Student(){
        System.out.println("调用无参的构造方法");
        count++;
    }
    public Student(int age){
this.age=age;
        System.out.println("调用只有一个参数的构造方法");
        this.age=age;
    }
    public  Student(int age,String name){
this.age=age;
this.name=name;
        System.out.println("调用有两个参数的构造方法");
    }
    public void printlnfo(){
        System.out.println(this.name+"的年龄是"+this.age);
    }
    public static void printCount(){
        System.out.println("当前Count = : " + count);
    }
    public static void main(String[] ages){
        Student s1 = new Student(20,"张三");
        Student s2 = new Student(20,"张三");
        s1.printlnfo();
        s2.printlnfo();
        //Student s3 = new Student(); 测试count
        Student.printCount();
    }
}


6.

public class Account {
    String account;
    String name;
    double balance;
    double money;
    double money1;
    static double minBalance=10;

   public  Account(String a,String n,double b)
    {
       account = a;
       name = n;
       balance =b;
    }
    public  void deposit(double money){
        this.balance+=money;
        System.out.println("原有金额为:"+(balance-money)+"现存金额为:"+money+"存入后的金额为:"+balance);
    }
    public void withdraw(double money1){
        if((this.balance-money1)





你可能感兴趣的:(java学习)