java基礎類和對象-題


1、創建一個三角形類,成員變量三邊,方法求周長,創建類主類A來測試它。

public class Sanjiaoxing {
    //定義屬性
    private int a;
    private int b;
    private int c;
    //getter、setter方法
    public int getA() {
        return a;
    }

    public void setA(int a) {
        this.a = a;
    }

    public int getB() {
        return b;
    }

    public void setB(int b) {
        this.b = b;
    }

    public int getC() {
        return c;
    }

    public void setC(int c) {
        this.c = c;
    }
    //成員方法
    public int Zhouchang(int a,int b,int c){
        return a+b+c;
    }
}

測試:

public class TestSanjiaoxing {

    public static void main(String[] args) {
        // TODO 自動生成的方法存根
        Sanjiaoxing a=new Sanjiaoxing();
        System.out.println("邊長為3,4,5的三角形的周長是:"+a.Zhouchang(3, 4, 5));
    }
}

 2、按要求編寫Java應用程序。

1)創建一個叫做People的類:

屬性:姓名、年齡、性別、身高

行為:說話、計算加法、改名

編寫能為所有屬性賦值的構造方法;

2)創建主類:

創建一個對象:名叫“張三”,性別“男”,年齡18歲,身高1.80

讓該對象調用成員方法:

說出“你好!”

計算23+45的值

將名字改為“李四”

public class People {
    private String name;
    private int age;
    private String sex;
    private double height;
    public void shuohua(){
        System.out.println(name+"說"+"你好!");
    }
    People(){
        
    }
    People(String name,int age,String sex,double height){
        setName(name);
        setAge(age);
        setSex(sex);
        setHeight(height);
    }
    public String getName() {
        return name;
    }

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

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    public double getHeight() {
        return height;
    }

    public void setHeight(double height) {
        this.height = height;
    }

    public int jisuan(int a,int b){
        return a+b;
    }
    public String gaiName(String name){
        setName(name);
        return getName();
    }
}
public class TestPeople {

    public static void main(String[] args) {
        // TODO 自動生成的方法存根
        People p=new People();
        People p2=new People("王五",18,"男",1.56);
        p.setName("張三");
        p.setSex("男");
        p.setAge(18);
        p.setHeight(1.80);
        System.out.println("名叫"+p.getName()+"性別"+p.getSex()+"年齡"+p.getAge()+"歲"+"身高"+p.getHeight());
        p.shuohua();
        System.out.println("23+45="+p.jisuan(23, 45));
        System.out.println("名字改為:"+p.gaiName("李四"));
        System.out.println("名叫"+p2.getName()+"性別"+p2.getSex()+"年齡"+p2.getAge()+"歲"+"身高"+p2.getHeight());
    }

}

3、按要求編寫Java應用程序。

1)創建一個叫做機動車的類:

屬性:車牌號(String),車速(int),載重量(double)

功能:加速(車速自增)、減速(車速自減)、修改車牌號,查詢車的載重量。

編寫兩個構造方法:一個沒有形參,在方法中將車牌號設置“XX1234”,速

度設置為100,載重量設置為100;另一個能為對象的所有屬性賦值;

2)創建主類:

在主類中創建兩個機動車對象。

創建第一個時調用無參數的構造方法,調用成員方法使其車牌為“遼

A9752”,並讓其加速。

創建第二個時調用有參數的構造方法,使其車牌為“B5086”,車速為150,

載重為200,並讓其減速。輸出兩輛車的所有信息

public class Jidongche {
    private String chepai;
    private int chesu;
    private double zaizhong;
//構造方法 對屬性賦值 Jidongche(){ chepai
="XX1234"; chesu=100; zaizhong=100; }
//構造方法帶形參 將實例化對象的參數用setter方法傳給屬性 Jidongche(String chepai,
int chesu,double zaizhong){ setChepai(chepai); setChesu(chesu); setZaizhong(zaizhong); }
//getter 、setter方法
public String getChepai() { return chepai; } public void setChepai(String chepai) { this.chepai = chepai; } public int getChesu() { return chesu; } public void setChesu(int chesu) { this.chesu = chesu; } public double getZaizhong() { return zaizhong; } public void setZaizhong(double zaizhong) { this.zaizhong = zaizhong; }
//成員方法 加速
public int jiasu(){ return chesu+=10; }
//成員方法 減速
public int jiansu(){ if(chesu<=0){ System.out.println("車停了!");
        return chesu; }
     else{   
return chesu-=10;
     } }
//成員方法 改車牌
public String gaichepai(String chepai){ setChepai(chepai); return getChepai(); }
//成員方法 查詢載重
public double chaxunzaizhong(){ return getZaizhong(); } }
public class TestJidongche {

    public static void main(String[] args) {
        // TODO 自動生成的方法存根
        Jidongche a=new Jidongche();
        System.out.println("車a車牌是:"+a.gaichepai("遼A9752"));
        System.out.println("車a加速后速度:"+a.jiansu());
        System.out.println("a車信息:"+a.getChepai()+" "+a.getChesu()+" "+a.getZaizhong());
        Jidongche b=new Jidongche("遼B5086",150,200);
        System.out.println("b車信息:"+b.getChepai()+" "+b.getChesu()+" "+b.getZaizhong());
        System.out.println("車b減速后速度:"+b.jiansu());
    }

}

4、創建一個Point類,有成員變量xy,方法getX(),setX(),還有一個構造方法初始化xy。創建類主類A來測試它。

public class Point {
    private int x;
    private int y;
    public int getX() {
        return x;
    }
    public void setX(int x) {
        this.x = x;
    }
    public int getY() {
        return y;
    }
    public void setY(int y) {
        this.y = y;
    }
    Point(int x,int y){
        setX(x);
        setY(y);
    }
    public int jia(){
        return x+y;
    }
}
public class TestPoint {

    public static void main(String[] args) {
        // TODO 自動生成的方法存根
        Point a=new Point(5,7);
        System.out.println("x="+a.getX());
        System.out.println("y="+a.getY());
        System.out.println("x+y="+a.jia());
    }

}

5、首先,編寫一個類ChongZai,該類中有3個重載的方法void print();其次,再編寫一個主類來測試ChongZai類的功能。

public class ChongZai {
    public int Max(int a, int b){
        return (a>=b) ? a : b;
    }
    public double Max(double a, double b){
        return (a>=b) ? a : b;
    }
    public double Max(double a, double b, double c){
        return Max(Max(a,b),c);
    }
    public void Min(int a,int b){
        int c=(a>=b) ? a : b;
        System.out.println(c);
    } 
    public void Min(double a,double b){
        double c=(a>=b) ? a : b;
        System.out.println(c);
    } 
    public void Min(double a,double b,double c){
        double sum=a+b+c;
        System.out.println(sum);
    } 
}
public class TestChongZai {

    public static void main(String[] args) {
        ChongZai chongzai=new ChongZai();
        System.out.println("最大值是:"+chongzai.Max(3, 5));
        System.out.println("最大值是:"+chongzai.Max(3.0, 5.3));
        System.out.println("最大值是:"+chongzai.Max(3.6, 5.4,8.9));
        chongzai.Min(5, 9);
        chongzai.Min(7.56, 4.56);
        chongzai.Min(1.234, 2.342, 7.3212);
    }

}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM