1. 程式人生 > >《java基礎入門課後習題》

《java基礎入門課後習題》

 private static void bobsort(int[] a) {
  for(int i=0;i<a.length-1;i++){
   for(int j=0;j<i;j++){
    if(a[j]>a[j+1]){
     int temp;
     temp=a[j];
     a[j]=a[j+1];
     a[j+1]=temp;
    }  
   }
  }
   }
}
---------------------------------------------------------
/**
 * 要求:設計一個student類,要求
 *   1:student類中包含姓名,成績兩個屬性;
 *   2:分別給這兩個屬性定義兩個方法,一個方法用於設定值,另一個方法用於獲取值;
 *   3;student 類中定義一個無引數的構造方法,和一個接受兩個引數的構造方法,兩個引數分別為姓名和成
 * 績屬性賦值。
 *   4:在測試類中建立兩個student物件,一個使用無引數的構造方法,然後呼叫方法給姓名和成績賦值,另
 * 一個使用有引數的構造方法,在構造方法中給姓名和成績賦值;
 */
package exercise;
class Student {
 String name;
 int grade;
 Student(){//無引數構造器
  System.out.println("恭喜你,建立物件成功");
 }
 Student(String name,int grade){//初始化姓名和成績的構造器
  this();
  this.name=name;
  this.grade=grade;
 }
 public  void setname(String name){//設定姓名
  this.name=name;
  
 }
 public void setgrade(int  grade){//設定成績
  this.grade=grade;
  
 }
 public void getname(){//獲取姓名
  System.out.println("姓名為:"+this.name);
 }
 public void getgrade(){//獲取成績
  System.out.println("成績為:"+this.grade);
 }
}
public class Chapter3_6_1{
 public static void main(String[] args){
  Student s1=new Student();
  s1.setname("張三");
  s1.setgrade(57);
  s1.getname();
  s1.getgrade();
  Student s2=new Student("李四",60);
  s2.getname();
  s2.getgrade();
 }
}
------------------------------------------------------
/**
 * 要求:定義一個father類和一個child類,要求
 *   1:father類為外部類,類中定義一個私有的string型別的屬性,name,name的值為zhangjun;
 *   2:child類為father類的內部類,其中定義一個introfather()方法,方法中定義father的name屬性
 *   3:定義一個測試的taste類,在其主函式的方法中建立child物件,並呼叫其方法;
 */
package exercise;
class Father{
 private String name;
 class Child{
   void introfather(){
   name="zhangjun";
   System.out.println("father的name屬性為:"+name);
  }
 }
}
public class Chapter3_6_2{
 public static void main(String [] args){
  Father.Child child=new Father().new Child();//建立內部類物件
  child.introfather();//內部類例項呼叫其方法
 }
}
----------------------------------------------------------------------
/**功能:設計一個學生類和它的子類,Undergraduate
   要求:1:Student類有name 和 age 屬性,一個包含兩個引數的構造方法,用於給name和age兩個屬性賦值一個show方法用於列印學生的屬性資訊。
         2:本科生類還有一個degree屬性,有一個包含三個引數的構造方法,前兩個用於給name和age賦值,第三個給degree專業賦值,一個show方法用於列印Ungergraduate學生的屬性資訊
         3:在測試類中分別建立Student類和Undergraduate物件,呼叫他們的show方法。*/
package exercise;
public class Chapter4_6_1{
 public static void main(String[] args){
  Students s=new Students("張三",20);
  s.show();
  Undergraduate us=new Undergraduate("李四",22,"本");
  us.show();
 }
}
/*學生類,具有學生初始化姓名,年齡以及顯示其姓名,年齡的方法*/
class Students{
 String name;
 int age;
 Students(String name,int age){
  this.name=name;
  this.age=age;
 }
 public void show(){
  System.out.println("名字:"+name+"......"+"年齡:"+age);
 }
}
/*本科生類,繼承自學生類,除學生類所具有的功能外,還具有初始化學歷,列印學歷的功能*/
class Undergraduate extends Students{
 String name;
 int age;
 private String degree;
 Undergraduate(String name,int age,String degree){
  super(name,age);
  this.degree=degree;
 }
 public void show(){
  super.show();
  System.out.println("學位:"+degree);
 }
}
-----------------------------------------------------------
/**功能:設計一個Shape 介面和他的兩個實現類Square和Circle
    要求1:Shape 介面中有一個抽象方法,area(),方法接收一個double型別的引數,返回一個double型別的結果。
  要求2:Square和Circle中實現了Shape介面,的area()方法,分別求正方形和圓形的面積並返回。
   在測試類中建立Square 和Circle物件,計算邊長為2的正方形和邊長為3的圓形的面積。*/
package exercise;
public class Chapter4_6_2{
 public static void main(String [] args){
  Square s=new Square();
  System.out.println(s.area(4.0));
  Circle c=new Circle();
  System.out.println(c.area(2.0));
 }
}
interface Shape{//定義的一個圖形介面
 
 public  abstract double area(double a);//介面中有一個求面積的抽象方法
}
class Square implements Shape{//正方形實現了圖形介面
 public double area(double a){
  return a*a;
 }
}
class Circle implements Shape{//圓實現了圖形介面
 public static final double PI=3.1415926;
 public double area(double r){
  return PI*r*r;
 }
}
--------------------------------------------
/**功能:自定義一個異常類,NoThisSoundException和player類,在Player的play()方法中使用自定義異常類
   要求:NoThisSoundException繼承自Exception類,類中有一個無參和接受一個引數的構造方法,構造方法中使用super關鍵字呼叫父類的構造方法,
           2:player類中定義一個play(int index)方法,方法接受一個int型的引數,表示播放歌曲的索引,當index>10時,play方法用throw關鍵字丟擲NoThisSongException異常,建立異常物件時,呼叫有參的構造方法,傳入“您播放的歌曲不存在”
         3:在測試類中建立player物件,並呼叫play()方法測試自定義的NoThisSongException,使用try....catch語句捕獲異常,呼叫NoThisSongException的getMessage()方法打印出異常資訊。*/
package exercise;
import java.lang.*;
public class Chapter4_6_3{
 public static void main(String [] args){
  Player p=new Player();//測試類中建立一個物件。
  try{
   p.play(11);//呼叫物件方法的時候捕獲異常
  }catch(NoThisSoundException e){
   System.out.println(e.getMessage());//將捕獲到的異常進行處理並列印相關資訊
  }
 }
}   
class NoThisSoundException extends Exception{//自定義一個類,繼承自exception
 NoThisSoundException(){//定義一個無參的構造方法
  super();
 }
 NoThisSoundException(String Message){//有參的構造方法
  super(Message);
  
 }
}
class Player{//定義一個Player類,
 int index;
 public void play(int index)throws NoThisSoundException{//在方法中宣告異常
  if(index>10) throw new NoThisSoundException("您播放的歌曲不存在");//發生異常時丟擲異常
 }
}