封装一个类MyStock存放在cn.com.my包中,这个类包括:

1 一个名为id的int数据字段表示股票代码;P96第四题

等等等

主函数中具体的数据可以自行修改


import cn.com.my.MyStock;

public class NewMain {

    public static void main(String[] args) {
        double a;
        MyStock MyStock = new MyStock();
        MyStock.previousPrice = 12.34;
        MyStock.currentPrice =12.45;
        MyStock.setid(123214);
        MyStock.setname("烟带" );
        System.out.println("id:"+MyStock.getid());
        System.out.println(MyStock.getname());
        System.out.printf("股票的变化:" + "%.5f", MyStock.getPriceChange());
        System.out.print('%');
    }
}

 在MyStack类中


package cn.com.my;

public class MyStock {

    int id;
    String name;
    public double previousPrice;
    public double currentPrice;

    public int getid() {
        return id;
    }


    public void setid(int id) {
        this.id = id;
    }

    public String getname() {
        return name;
    }

    public void setname(String name) {
        this.name = name;
    }

    public double getpreviousPrice() {
        return previousPrice;
    }

    public void setpreviousPrice(double previousPrice) {
        this.previousPrice = previousPrice;
    }

    public double getcurrentPrice() {
        return currentPrice;
    }

    public void setcurrentPrice(double currentPrice){
        this.currentPrice=currentPrice;
    }

    public double getPriceChange() {
        double a;

        a = ((currentPrice - previousPrice) / previousPrice) * 100;

        return (a);
    }
}

 一个有特定代码和名称的股票构造方法就是上述中构造中的多个get和set,set是用来从主函数中获得数据的,get是把得到的数据输出到主函数中的。在主函数中若要直接引用MyStock中的数据的话,需要把MyStock中的数据public。

2.封装一个类MyAccount类存放在cn.com.my包中:

主函数与1中类似稍微修改模拟银行开户过程

import cn.com.my.MyAccount;
public class NewMain {
    public static void main(String[] args) {
        System.out.println("正在创建新账户……");
        MyAccount a = new MyAccount();
        a.setId(4123);
        a.setBalance(0);
        a.setAnnualRate(0.1);
        System.out.println("账户ID为:" + a.getId());
        System.out.println("账户余额为:" + a.getBalance());
        System.out.println("开户日期为:" + a.getDateCreated());
        System.out.println("月利率为" + a.getAnnualRate() + "%");
        a.deposit(123);
        System.out.println("您当前的余额为" + a.getBalance());
        a.withDarw(2);
        System.out.println("您当前的余额为" + a.getBalance());
    }
}

而在MyAccount中具体的代码如下


package cn.com.my;

import java.util.Date;

public class MyAccount {
    public MyAccount(){

    }
    public MyAccount(int a){
        this.id=1;
        this.balance=9;
    }
    int id=0;
    double balance;
    double annualRate=0;

    Date dateCreated=new Date();
    public void setId(int id){
        this.id=id;
    }
    public int getId(){
        return id;
    }
    public void setBalance(double balance){
        this.balance=balance;
    }
    public double getBalance(){
        return balance;
    }
    public void setAnnualRate(double annualRate){
        this.annualRate=annualRate;
    }
    public double getAnnualRate(){
        return annualRate;
    }
    public Date getDateCreated(){
        return dateCreated;
    }

    public double getMonthRate(){
        return 0.25;
    }
    public double withDarw(int n){
        balance=balance-n;
        return balance;
    }
    public double deposit(int n){
        balance=balance+n;
        return balance;
    }
}

封装一个MyPoint存放在cn.com.my包中,这个类包括:

1.两个double类型的私有坐标点x和y

等等等

主程序为

import cn.com.my.MyPoint;

public class NewMain {
    public static void main(String[] args) {
        MyPoint first = new MyPoint();
        MyPoint second= new MyPoint(1);
        System.out.println("MyPoint类型的两个点的距离为:"+MyPoint.distance(first, second));
        MyPoint b1=new MyPoint();
        MyPoint b2=new MyPoint();
        b1.setX(1.1);
        b1.setY(1.2);
        b2.setX(2.3);
        b2.setY(2.5);
        System.out.printf("指定x和y坐标的两个点之间的距离为:"+MyPoint.distance(b1.getX(), b1.getY(), b2.getX(), b2.getY()));
    }
}

MyPoint类里面

package cn.com.my;

public class MyPoint {
    public MyPoint(){
        x=0;
        y=0;
    }
    public MyPoint(int a){
        x=1.1;
        y=2.1;
    }
    double x,y;
    public void setX(double x){
        this.x=x;
    }
    public double getX(){
        return x;
    }
    public void setY(double y){
        this.y=y;
    }
    public double getY(){
        return y;
    }
    public static double distance(MyPoint a,MyPoint b){
        double x=(a.x-b.x)*(a.x-b.x);
        double y=(a.y-b.y)*(a.y-b.y);
        double c=Math.sqrt(x+y);
        return c;
    }
    public static double distance(double x1,double y1,double x2,double y2){
        double x=(x1-x2)*(x1-x2);
        double y=(y1-y2)*(y1-y2);
        double c=Math.sqrt(x+y);
        return c;
    }

}

Beautiful

整个Java要比C更加美

 posted on 2022-09-07 20:57  ZZC12312321  阅读(79)  评论(0编辑  收藏  举报  来源