Java方法&面向對象習題


1、編寫一個無參方法,輸出Hello。

package Dongruan;

 

public class ktlx1 {

    public static void main(String[] args) {

       print();

    }

    public static void print(){

       System.out.println("hello!");

    }

 

}

 

2、編寫一個得到兩個int型參數方法,輸出兩個參數的和。

package Dongruan;

 

public class ktlx1 {

    public static void main(String[] args) {

       int a=2;

       int b=3;

       print(a,b);

    }

    public static void print(int a,int b){

       System.out.println(a+b);

    }

 

}

 

3、編寫一個得到兩個int型參數方法,返回兩個參數的和。

package Dongruan;

public class ktlx1 {

    public static void main(String[] args) {

       double a=print(2,3);

       System.out.println(a);

    }

    public static  double  print(double a,double b){

      

       return a+b;

    }

}

 

 

1、編寫一個方法,求整數n的階乘,例如5的階乘是1*2*3*4*5。 [必做題]

 

 

 

package Dongruan;

import java.util.Scanner;

public class ktlx1 {

    public static void main(String[] args) {

       int a=print(5);

       System.out.println(a);

    }

    public static  int   print(int a){

       int sum=1;

       for(int i=1;i<=a;i++){

           sum=sum*i;

       }

       return sum;

    }

}

 

2、編寫一個方法,判斷該年份是平年還是閏年。[必做題]

package Dongruan;

import java.util.Scanner;

public class ktlx1 {

    public static void main(String[] args) {

       print(2020);

    }

    public static  int   print(int year){

       if(year%4==0&&year%100!=0||year%400==0){

           System.out.println("閏年");

       }else{

           System.out.println("平年");

       }

       return year;

    }

}

 

1、編寫一個方法,輸出大於200的最小的質數。[選做題]

 

 

package Dongruan;

import java.util.Scanner;

 

import java.util.Scanner;

 

public class ktlx1 {

 

       public static void main(String[] args) {

      

      for(int i=201;i<300 ; i++){

             if(i%2!=0&&i%3!=0&&i%5!=0&&i%7!=0&&i%9!=0){

                    System.out.println(i);

                    break;

             }

      }                             

       }

}

 

 

2、寫一個方法,功能:定義一個一維的int 數組,長度任意,然后將它們按從小到大的順序輸出(使用冒泡排序)(知識點:方法的定義和訪問)。[選做題]

package Dongruan;

import java.util.Scanner;

public class CopyOfktlx2 {

    public static void main(String[] args) {

       int [] arr={12,3,56,1,0,10,56};

       print(arr);

      

    }

    public static  void   print(int[] arr){

      

       for(int i=1;i<arr.length;i++){

           for(int j=0;j<arr.length-i;j++){

              if(arr[j]<arr[j+1]){

                  int temp=arr[j];

                  arr[j]=arr[j+1];

                  arr[j+1]=temp;

              }

           }

       }

       for(int i=1;i<arr.length;i++){

              System.out.println(arr[i]);

       }

       System.out.println("\n");

          

      

      

    }

}

 

第六章

根據需求編寫類:

西游記游戲軟件中的游戲人物包括:

孫悟空:孫悟空的武器是金箍棒,戰斗力五顆星,耐力五顆星

唐  僧:唐僧沒有武器,戰斗力為零,耐力五顆星

豬八戒:豬八戒的武器是耙子,戰斗力四顆星,耐力兩顆星

沙  僧:沙僧的武器是月牙鏟 ,戰斗力三顆星,耐力四顆星

要求根據需求抽象出屬性和方法,編寫類實現。

package Dongruan;

 

public class xiyouji {

    private String weapon;

    private String zdl;

    private String nl;

    private String name;

   

    public String getName() {

       return name;

    }

    public void setName(String name) {

       this.name = name;

    }

    public String getWeapon() {

       return weapon;

    }

    public void setWeapon(String weapon) {

       this.weapon = weapon;

    }

    public String getZdl() {

       return zdl;

    }

    public void setZdl(String zdl) {

       this.zdl = zdl;

    }

    public String getNl() {

       return nl;

    }

    public void setNl(String nl) {

       this.nl = nl;

    }

    public void show1(){

       System.out.println(getName()+"武器:"+getWeapon()+",戰斗力:"+getZdl()+",耐力:"+getNl());

    }

   

   

   

 

}

 

測試類

package Dongruan;

 

public class testxiyouji {

    public static void main(String[] args) {

       xiyouji x1=new xiyouji();

       x1.setName("孫悟空      ");

       x1.setWeapon("金箍棒");

       x1.setZdl("五顆星");

       x1.setNl("五顆星");

        x1.show1();

       

       

        xiyouji x2=new xiyouji();

       x1.setName("唐僧         ");

       x1.setWeapon("念經");

       x1.setZdl("0");

       x1.setNl("五顆星");

        x1.show1();

        

        xiyouji x3=new xiyouji();

       x1.setName("豬八戒      ");

       x1.setWeapon("九齒釘耙");

       x1.setZdl("四顆星");

       x1.setNl("二顆星");

        x1.show1();

       

        xiyouji x4=new xiyouji();

       x1.setName("沙僧         ");

       x1.setWeapon("月牙鏟");

       x1.setZdl("三顆星");

       x1.setNl("四顆星");

        x1.show1();

      

    }

}

編寫一個三角圖形類,有三個屬性分別代表三邊長度。

編寫屬性要求如下:

邊長必須為正數

三個邊長必須能組合成三角形(三角形任意兩邊和大於第三邊)

編寫方法要求如下:

對邊長進行賦值

輸出三角形的三個邊長

編寫主函數,對該三角圖形類進行調用。

package Dongruan;

 

public class sanjiaoxing {

    int bc1;

    int bc2;

    int bc3;

    public int getBc1() {

       return bc1;

    }

    public void setBc1(int bc1) {

       if(bc1<0){

           System.out.println("你家邊長能小於零?");

       }

       this.bc1 = bc1;

    }

    public int getBc2() {

       return bc2;

    }

    public void setBc2(int bc2) {

       if(bc2<0){

           System.out.println("你家邊長能小於零?");

       }

       this.bc2 = bc2;

    }

    public int getBc3() {

       return bc3;

    }

    public void setBc3(int bc3) {

       if(bc2<0){

           System.out.println("你家邊長能小於零?");

       }

       this.bc3 = bc3;

    }

    public void show(){

       if((bc1+bc2)>bc3&&(bc2+bc3)>bc1&&(bc3+bc1)>bc2){

           System.out.println("三邊之和"+bc1+bc2+bc3);

           System.out.println("bc1:"+bc1+"\nbc2:"+bc2+"\nbc3:"+bc3);

       }

    }

}

 

測試類

package Dongruan;

public class testsanjiaoxing {

    public static void main(String[] args) {

       sanjiaoxing s=new sanjiaoxing();

        s.setBc1(0);

        s.setBc2(3);

        s.setBc3(5);

        s.show();

    }

}

main打印static 與非 static 區別

package Dongruan;

public class CircleStatic{

    static double pi = 3.14;

    int radius=100;

    public static void main(String[ ] args){

       System.out.println(pi);  //打印pi       

       CircleStatic c = new CircleStatic();

       System.out.println(c.radius); //打印radius  

    }

}

 

1、 定義一個點類Point,包含2個成員變量x、y分別表示x和y坐標,2個構造器Point()和Point(int x0,y0),以及一個movePoint(int dx,int dy)方法實現點的位置移動,創建兩個Point對象p1、p2,分別調用movePoint方法后,打印p1和p2的坐標。[必做題]

測試類

package Dongruan;

 

public class testpoint {

    public static void main(String[] args) {

       point p1=new point();

       p1.movepoint(3, 4);

       point p2=new point();

       p2.movepoint(5, 8);

       point p3=new point(12,34);

       p3.point1();

    }

}

 

package Dongruan;

 

public class point {

    int x;

    int y;

    public point(){}

    public point(int x0,int y0){

       this.x=x0;

       this.y=y0;

      

    }

    public void point1(){

       System.out.println("無參構造("+x+","+y+")");

    }

    public void movepoint(int dx,int dy){

       System.out.println("坐標是:("+dx+","+dy+")");

    }  

}

2、定義一個矩形類Rectangle: [必做題]

2.1  定義三個方法:getArea()求面積、getPer()求周長,showAll()分別在控制台輸出長、寬、面積、周長。

2.2  有2個屬性:長length、寬width

2.3  通過構造方法Rectangle(int width, int length),分別給兩個屬性賦值

2.4  創建一個Rectangle對象,並輸出相關信息

package Dongruan;

 

public class rectangle {

    int length;

    int width;

    public rectangle(int length,int width){

       this.length=length;

       this.width=width;

    }

    //面積

    public void getarea(){

       //return length*width;

      System.out.println(length*width);

    }

    //周長

    public void getper(){

       //return 2*(length+width);

        System.out.println(2*(length+width));

    }

    //showall

    public void showall(){

        System.out.println("長:"+length+",寬:"+width+"面積:"+length*width+"周長:"+2*(length+width));

    }

 

 

}

 

 

測試類

package Dongruan;

 

public class testrectangle {

    public static void main(String[] args) {

       rectangle r=new rectangle(2, 3);

       r.getarea();

       r.getper();

       r.showall();

    }  

}

3、定義一個筆記本類,該類有顏色(char)和cpu型號(int)兩個屬性。 [必做題]

3.1 無參和有參的兩個構造方法;有參構造方法可以在創建對象的同時為每個屬性賦值;

3.2  輸出筆記本信息的方法

3.3  然后編寫一個測試類,測試筆記本類的各個方法。

package Dongruan;

public class bijiben {

    char color;

    int cpu;

    public bijiben(){}

    public bijiben(char color,int cpu){

       this.color=color;

       this.cpu=cpu;

    }

    public  void print(){

       System.out.println("筆記本信息: 顏色:"+color+",cpu型號"+cpu);

      

    }

 

}

測試類

package Dongruan;

public class testbijiben {

    public static void main(String[] args) {

       bijiben b=new bijiben('黑',15);

       b.print();

    }

}

 

4、定義兩個類,描述如下: [必做題]

4.1定義一個人類Person:

4.1.1定義一個方法sayHello(),可以向對方發出問候語“hello,my name is XXX”

4.1.2有三個屬性:名字、身高、體重

4.2定義一個PersonCreate類:

4.2.1創建兩個對象,分別是zhangsan,33歲,1.73;lishi,44,1.74

4.2.2分別調用對象的sayHello()方法。

package Dongruan;

 

public class person {

    String name;

    double height;

    int weight;

    int age;

   

   

    public void sayhello(){

       System.out.println("Hello!my name is "+name+",身高:"+height+",體重:"+weight+",年齡:"+age);

    }

    public int getAge() {

       return age;

    }

    public void setAge(int age) {

       this.age = age;

    }

   

   

    public String getName() {

       return name;

    }

    public void setName(String name) {

       this.name = name;

    }

    public double getHeight() {

       return height;

    }

    public void setHeight(double height) {

       this.height = height;

    }

    public int getWeight() {

       return weight;

    }

    public void setWeight(int weight) {

       this.weight = weight;

    }

 

}

 

測試類

package Dongruan;

 

public class testperson {

    public static void main(String[] args) {

       person p=new person();

       p.setName("張三");

       p.setAge(33);

       p.setHeight(1.73);

       p.setWeight(120);

       p.sayhello();

      

    }

 

}

 

5、定義兩個類,描述如下: [必做題]

5.1定義一個人類Person:

5.1.1定義一個方法sayHello(),可以向對方發出問候語“hello,my name is XXX”

5.1.2有三個屬性:名字、身高、體重

5.1.3通過構造方法,分別給三個屬性賦值

5.2定義一個Constructor類:

5.2.1創建兩個對象,分別是zhangsan,33歲,1.73;lishi,44,1.74

5.2.2分別調用對象的sayHello()方法

package Dongruan;

 

public class person {

    String name;

    double height;

    double weight;

    int age;

   

   

    person(String name, double height, double d, int age) {

       super();

       this.name = name;

       this.height = height;

       this.weight = d;

       this.age = age;

    }

    public void sayhello(){

       System.out.println("Hello!my name is "+name+",身高:"+height+",體重:"+weight+",年齡:"+age);

    }

    public int getAge() {

       return age;

    }

    public void setAge(int age) {

       this.age = age;

    }

   

   

    public String getName() {

       return name;

    }

    public void setName(String name) {

       this.name = name;

    }

    public double getHeight() {

       return height;

    }

    public void setHeight(double height) {

       this.height = height;

    }

    public double getWeight() {

       return weight;

    }

    public void setWeight(int weight) {

       this.weight = weight;

    }

 

}

 

 

測試類

package Dongruan;

 

public class testperson {

    public static void main(String[] args) {

       person p=new person("張三",33,1.73,120);

      

       p.sayhello();

      

    }

}

1、 設計一個類Student,該類包括姓名、學號和成績。設計一個方法,按照成績從高到低的順序輸出姓名、學號和成績信息。[選做題]

2、 package Dongruan;

3、  

4、 public class student1 {

5、 private String name;

6、 private int id;

7、 private int score;

8、 public String getName() {

9、     return name;

10、         }

11、         public void setName(String name) {

12、            this.name = name;

13、         }

14、         public int getId() {

15、            return id;

16、         }

17、         public void setId(int id) {

18、            this.id = id;

19、         }

20、         public int getScore() {

21、            return score;

22、         }

23、         public void setScore(int score) {

24、            this.score = score;

25、         }

26、         public  student1(String name, int id, int score) {

27、            super();

28、            this.name = name;

29、            this.id = id;

30、            this.score = score;

31、         }

32、         public void show() {

33、         System.out.println("id:"+id+",name="+name+",score="+score);

34、         }

35、         public static void sort(student1[] stus){

36、            student1 stu;

37、            for(int i=0;i<stus.length-1;i++){

38、                for(int j=0;j<stus.length-i-1;j++){

39、                   if(stus[j].score>stus[j+1].score){

40、                       stu=stus[j];

41、                       stus[j]=stus[j+1];

42、                       stus[j+1]=stu;

43、                   }

44、                }

45、            }

46、         }

47、        

48、        

49、      

50、     }

51、             

測試類

package Dongruan;

 

public class teststudent {

    public static void main(String[] args) {

       student1[] stus=new student1[3];

        stus[1]=new student1("zhangsan",1,88);

        stus[0]=new student1("lisi",2,99);

        stus[2]=new student1("wangwu",3,100);

       student1.sort(stus);

       for(student1 stu:stus){

           stu.show();

       }

    }

   

 

}

 

 

2、定義一個汽車類Vehicle,要求如下:[選做題]

2.1屬性包括:汽車品牌brand(String類型)、顏色color(String類型)和速度speed(double類型),並且所有屬性為私有。

2.2至少提供一個有參的構造方法(要求品牌和顏色可以初始化為任意值,但速度的初始值必須為0)。

2.3為私有屬性提供訪問器方法。注意:汽車品牌一旦初始化之后不能修改。

2.4定義一個一般方法run(),用打印語句描述汽車奔跑的功能

2.5定義測試類VehicleTest,在其main方法中創建一個品牌為“benz”、顏色為“black”的汽車。

package Dongruan;

 

public class vehicle {

    private String brand;

    private String color;

    private double speed;

    public vehicle(String brand,String color){

       this.brand=brand;

       this.color=color;

      

    }

 

    vehicle(String brand, String color, double speed) {

       super();

       this.brand = brand;

       this.color = color;

       this.speed = speed;

    }

    public void run(){

       System.out.println("這個汽車的品牌為"+this.brand+"這個汽車的顏色為"+this.color+"這個汽車的速度為"+this.speed);

    }

   

 

}

 

測試類

package Dongruan;

 

public class testvehicle {

    public static void main(String[] args) {

       vehicle v=new vehicle("benz","black");

       v.run();

       vehicle v1=new vehicle("benz","black",13);

       v1.run();

    }

   

 

}

 


免責聲明!

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



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