2017年6月24日 星期六

第九章 傳回星期

題目:撰寫一個程式,其中包含一個類別Dates,並再建構方法中初始化一個包含有七個元素的字串陣列,

各個元素對應到星期一到星期天的英文縮寫,

並提供一個方法askDate(),並傳入1~7的數字,傳回對應的英文縮寫 

import java.util.Scanner;
class Dates{
String []day={"Mon", "Tue", "Wed", "Thu", "Fri","Sat" ,"Sun"};
void askDate(int i){

System.out.print(day[i-1]);
}
}


public class HomeWork0901 {

public static void main(String[] args) {
int i;
   loop:
   while (true){//檢查是否1~7不然要重新輸入一次
     System.out.print("1~7-> ");
     Scanner sc=new Scanner(System.in);
   
      i=sc.nextInt();
   
      if (i>=1 && i<=7){
      sc.close();
        break;
      }
      continue loop;
   }

Dates day =new Dates();

day.askDate(i);

}

}

第八章 QuickSort排序法 由大排到小


題目:使用Ellis Horowitz法,將陣列由大到小依序排列


class Sorter1 {
int[] data;

void quickSort(int start,int end) {

if(start >= end) {
return;
}

int mid = data[(start + end) / 2];

int left = start;
int right = end;
while(true) {
while(data[left] > mid) {
left++;
}
while(data[right] < mid) { //
right--;
}
if(left < right) { //
int temp = data[left]; //
data[left] = data[right];
data[right] = temp;
left++; //
right--; //
show();
}
else //
break;
}

  quickSort(start,left-1);
  quickSort(right+1,end);
  }

  void show() {
  for(int i:data) {
  System.out.print(i +" ");
  }
  System.out.println("");
  }

  void sort(int[] data) {
  this.data = data;
  show();
  quickSort(0,data.length - 1);
  }
 }

 public class HomeWork0810 {

 public static void main(String[] argv) {


int[] data = new int[argv.length];


for(int i = 0;i < data.length;i++) {
data[i] = java.lang.Integer.parseInt(argv[i]);
}

Sorter1 s = new Sorter1();

s.sort(data);
  }
 }

2017年6月23日 星期五

第八章 用MyCalc類別回傳特定三種數值

題目:撰寫一個MyCalc的Class,可以由Keyboard輸入一個數值n,然後分別呼叫3種方法來計算並顯示下列三種結果:
1.
1/1+1/2+1/3+...1/n
2.
1+2+3+...+n
3.
1~n之中可以被13整除的數字

import java.util.Scanner;
class MyCalc{
public static double harmonic (int n){
double sum=0;
for(double b=1;b<=n;b++)
sum+=(1/b);

System.out.println(sum);
return 0;
}
public static double triangular  (int n){
int  sum=0;
for(int j=1;j<=n;j++)
sum+=j;
System.out.println(sum);
return 0;
}

public static double Div13(int n){
for(int k=1;k<=n;k++){
if(k%13==0)
System.out.print(k+" ");
}
return 0;
}
}

public class HomeWork0809 {

public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
MyCalc.harmonic(n);
MyCalc.triangular(n);
MyCalc.Div13(n);
sc.close();

}

}

第八章 傳入陣列並計算其平均值

題目: 撰寫部論是傳入Int陣列或Float陣列,皆可以傳回陣列內所有元素平均值的方法

import java.util.Scanner;
class testAVG{
public static float intAVG(int []a,float []b){
float sum=0;

for(int i=0;i<a.length;i++){
sum+=a[i];
}
System.out.print(sum/a.length);
return sum/(a.length-1);

}
public static float floatAVG(int []a,float []b){
float sum=0;

for(int i=0;i<b.length;i++){
sum+=b[i];
}
System.out.print(sum/b.length);
return sum/(b.length-1);

}

}
public class HomeWork0808 {

public static void main(String[] args) {

System.out.println("請選擇\n1.整數\n2.浮點數");
Scanner sc=new Scanner(System.in);
int i=sc.nextInt();

if(i==1)
{
System.out.print("請輸入陣列長度");
int j=sc.nextInt();

int a[]=new int[j];
for(int k=0;k<j;k++){
a[k]=sc.nextInt();
}

float b[]=null;
testAVG.intAVG(a,b);
}
else if(i==2)
{
System.out.print("請輸入陣列長度");
int k=sc.nextInt();
float b[]=new float[k];
for(int m=0;m<k;m++){
b[m]=sc.nextFloat();
}
int a[]=null;
testAVG.floatAVG(a,b);
}
else
System.out.print("輸入錯誤");

sc.close();

}

}

2017年6月21日 星期三

第八章 傳入陣列並加總其值

題目:
請使用多重定義的技巧,撰寫不論是傳入整數陣列,並將陣列內容個元素的加總傳回

class test{
public static void show(int[] b){
int sum=0;

for(int i=0;i<b.length;i++){
sum+=b[i];
}
System.out.print(sum);
return;
}
}

public class HomeWork0807 {

public static void main(String[] args) {
   
   System.out.println("陣列內各元素總和為");
   test.show(new int[] {1,2,3,4,5});

}

}

2017年6月20日 星期二

第六章 中獎查詢

題目:預先設定六個介於1~49之間中獎號碼,並讓使用者輸入六個1~49之間的號碼。如果兩者相同就顯示中獎的訊息


import java.util.*;
public class HomeWork0608 {
public static void main(String[] args){
Scanner sc=new
Scanner(System.in);

int []envoice={3,5,18,28,33,40};//事先排好中獎號碼
int []check=new int[6];

for(int j=0;j<6;j++){
check[j]=sc.nextInt();
}

for (int i=0;i<check.length-1;i++){
for (int j = 0; j < check.length-1-i; j++){
if(check[j]>check[j+1]){
check[j]=check[j]^check[j+1];
check[j+1]=check[j]^check[j+1];
check[j]=check[j]^check[j+1];
}//使用XOR交換,未使用到變數儲存變數
}
}
//輸入內容,並以氣泡法排序

for(int t=0;t<6;t++){
if(check[t]!=envoice[t]){
System.out.print("未中獎");
break;
}
else
System.out.print("恭喜中獎");
break;
}//開始查詢是否中獎


sc.close();


}


}

2017年6月19日 星期一

第八章 河內塔計次版

題目:顯示盤子的移動狀況,並且在完成移動後輸出移動次數

import java.io.*;
class HanoiTowerGame{
static int count1=0;//累積移動次數用

void go(int discs){
hanoiTower('A','C','B',discs);
}

//實際搬動盤子
void moveDisc(char source, char target, int disc){
System.out.println("將"+disc+"號碟子從柱子"+
source+ "搬到"+target);
count1++;
}

/**
a: 來源柱子
b: 目的地
c: 空的柱子
discs: 碟子數量
*/

void hanoiTower(char a,char c, char b ,int discs){
if(discs==1){
moveDisc(a, c, discs);
return;
}

//先將最大碟子以外的碟子搬到B
hanoiTower(a, b, c, discs-1);

//把最大的碟子搬到C
moveDisc(a,c,discs);

//將搬到B的碟子搬到C
hanoiTower(b, c, a, discs-1);
}
}
public class Hanoi {

public static void main (String [] argv)throws IOException{
HanoiTowerGame game =new HanoiTowerGame();

BufferedReader br=
new BufferedReader(new InputStreamReader(System.in));

System.out.println("請輸入碟子數量");
int discs= java.lang.Integer.parseInt(br.readLine());

game.go(discs);

System.out.print(HanoiTowerGame.count1);

}
}

第八章 Iterative版 費式數列

題目:用"非"遞迴版寫出"Fibonacci"數列
import java.io.*;

class machematics{//Iterative approach
int fibonacci(int n){
if(n==0){
return 0;
}

else if(n==1){
return 1;
}
else
{
int a=0,b=1,c = 0;

for(int i=2;i<=n;i++){
c=a+b;
a=b;
b=c;
}
return c;
}
}
}

public class FibonacciIterative {

public static void main(String[] args) throws IOException{

machematics m= new machematics();

BufferedReader br =new
BufferedReader(new InputStreamReader(System.in));

System.out.print("請輸入n:");

int n=Integer.parseInt(br.readLine());

System.out.println("Fibonacci數列第"+n+"項:"+m.fibonacci(n));


}

}

第八章 猜動物的叫聲

題目:在main()中用(int)Math.random()*4,隨機選取四個動物中的其中一個,然後依據答對與否在程式中輸出「答對(錯了),喵嗚~我是貓!」

import java.util.Scanner;
public class RadomAnimal0804 {

public static void main(String[] args) {
String animalType[]={"dog","cat","cattle","horse"};
String animalSound[]={"bark","mew","moo","neigh"};
int i=(int) (Math.random()*4);


if(i==0){
Animal animal_1=new Animal();
animal_1.type=animalType[0];
animal_1.voice="bark";
animal_1.sound2();
System.out.print("...");
}

else if(i==1){
Animal animal_2=new Animal();
animal_2.type=animalType[1];
animal_2.voice="mew";
animal_2.sound2();
System.out.print("...");

}

else if(i==2){
Animal animal_3=new Animal();
animal_3.type=animalType[2];
animal_3.voice="moo";
animal_3.sound2();
System.out.print("...");

}

else if(i==3){
Animal animal_4=new Animal();
animal_4.type=animalType[3];
animal_4.voice="neigh";
animal_4.sound2();
System.out.print("...");

}

System.out.println("\n請依照動物的叫聲輸入正確的動物的英文名稱");

Scanner sc=new Scanner(System.in);

String input=sc.nextLine();

sc.close();

if(input.equals(animalType[i])){
System.out.print("答對了,"+animalSound[i]+"~");
System.out.print("我是"+animalType[i]+"!");
}
else
{
System.out.print("答錯了,"+animalSound[i]+"~");
System.out.print("我是"+animalType[i]+"!");
}

}
}

第八章 聽取動物的叫聲

題目:在main()中改用陣列來儲存4隻動物,用命令提示列傳入一個到多個參數 。
Ex輸入「java MyAnimal2 牛 狗」後,程式就要輸出「哞喔...旺旺」。如果不是上述所說的動物,則輸出「??...」

public class MyAnimal2 {
public static void main(String[] argv){
String animalType[]={"dog","cat","cattle","horse"};

for(int i=0;i<argv.length;i++){

if(argv[i].equals(animalType[0])==true){
Animal animal_1=new Animal();
animal_1.voice="bark";
animal_1.sound2();
System.out.print("...");
}

else if(argv[i].equals(animalType[1])==true){
Animal animal_2=new Animal();
animal_2.voice="mew";
animal_2.sound2();
System.out.print("...");

}

else if(argv[i].equals(animalType[2])==true){
Animal animal_3=new Animal();
animal_3.voice="moo";
animal_3.sound2();
System.out.print("...");
}

else if(argv[i].equals(animalType[3])==true){
Animal animal_4=new Animal();
animal_4.voice="neigh";
animal_4.sound2();
System.out.print("...");

}

else
System.out.print("??...");

}
/** 這個也可以讓程式正常運作,但是沒有用到animalType的陣列
*
for(int i1=0;i1<argv.length;i1++){
switch (argv[i1]){
case "dog":
Animal animal_1=new Animal();
animal_1.voice="bark";
animal_1.sound2();
System.out.print("...");
break;

case "cat":
Animal animal_2=new Animal();
animal_2.voice="mew";
animal_2.sound2();
System.out.print("...");
break;

case "cattle":
Animal animal_3=new Animal();
animal_3.voice="moo";
animal_3.sound2();
System.out.print("...");
break;

case "horse":
Animal animal_4=new Animal();
animal_4.voice="neigh";
animal_4.sound2();
System.out.print("...");
break;

default:
System.out.print("??...");
break;
**/




}
}

第八章 馬牛叫聲

題目:延續上題,將上提的main()移動到MyAnimal的類別中,將程式另存為MyAnimal.java。在main()增加兩隻動物「牛 哞喔」、「馬 沒嘿」 ,並讓兩隻動物各叫一聲

public class Animal { //HomeWork0801

String type;
String voice;

void sound(){
System.out.println(type+"、"+voice);
}

}

public class MyAnimal {

public static void main(String[] args) {

Animal cat=new Animal();
Animal dog=new Animal();
Animal cattle=new Animal();
Animal horse=new Animal();

cat.type="cat";
cat.voice="mew";
dog.type="dog";
dog.voice="bark";
cattle.type="cattle";
cattle.voice="moo";
horse.type="horse";
horse.voice="neigh";


cat.sound();
dog.sound();
horse.sound();
cattle.sound();

}
}

第八章 貓狗叫聲

題目:在main()的類別命名為"Animal",在內用String宣告叫聲的屬性,以及一個發出叫聲的方法。
接著再main()中建立兩個Animal物件,使分別發出 「狗 旺旺」&「貓 喵嗚」,然後讓兩隻動物叫一聲看看

注意:1~4題是連貫題 要一起看(使用)

public class  Animal { //HomeWork0801

String type;
String voice;

void sound(){
System.out.print(type+"、");
System.out.println(voice);
}

void sound2(){
System.out.print(voice);
}

void sound1(String s){
System.out.print(s);
}

public static void main(String[] args) {

Animal cat=new Animal();
Animal dog=new Animal();

cat.type="cat";
cat.voice="mew";
dog.type="dog";
dog.voice="bark";

cat.sound();
dog.sound();

}
}

第七章 陣列分割

題目:字串的Split("分割字串")法,可分割字串為一個陣列。Ex array的值"d,e,f",則array.split(",")會回傳{"a","b","c"}
請使用者輸入一字串,內容含有逗號分開的多項整數,將此分隔為陣列,然後依數值大小做遞增排列,再以逗號分開並顯示出來


import java.io.*;
public class HomeWork0710 {

public static void main(String[] args) throws IOException {
BufferedReader br = new
BufferedReader(new InputStreamReader(System.in));

System.out.println("請輸入以逗號分隔的多項整數:");
String str =br.readLine();
String []arr=str.split(",");//分割後存入陣列

int[] data=new int[arr.length];
    for (int i=0;i<arr.length;i++){
      int temp=Integer.parseInt(arr[i]);
      data[i]+=temp;
    }
    /**將陣列內容轉換,並
            另存成另一個Int格式的陣列  */
   
    System.out.println("輸入的資料");
   
    for(int j=0;j<data.length;j++){
    System.out.print(data[j]+" ");
    }//印出輸入的資料
   
    System.out.println();
    int temp=0;
    for(int i=0;i<data.length-1;i++)
    for(int j=0;j<data.length-i-1;j++){
    if(data[j]>data[j+1]){
    temp=data[j];
    data[j]=data[j+1];
    data[j+1]=temp;  
    }
    }
   
    System.out.println("輸出的資料");
   
    for(int j=0;j<data.length-1;j++){
    System.out.print(data[j]+", ");
    }
    System.out.print(data[data.length-1]);
//排序後的資料
}
}

第七章 氣泡排序法 XOR版 遞減版


public class HomeWork0709 {
public static void main(String[] argv){

int []array=new int[argv.length];

for(int i=0;i<argv.length;i++){
array[i]=Integer.parseInt(argv[i]);
System.out.print(array[i]+" ");
}
System.out.println();

for (int i=0;i<array.length-1;i++){
for (int j = 0; j < array.length-1-i; j++){
if(array[j]<array[j+1]){
array[j]=array[j]^array[j+1];
array[j+1]=array[j]^array[j+1];
array[j]=array[j]^array[j+1];
}//使用XOR交換,未使用到變數儲存變數
}
}//遞減排序,由大排到小(由左到右)排序


for(int j=0;j<array.length;j++){
System.out.print(array[j]+ " ");
}
}
}

第七章 停車場計費

題目:
使用二維陣列儲存時段與費率
並使用命令提示列傳入停車時數

1hr以內 30元/hour
2hr以內 50元/hours
3hr以內 70元/hours
5hr以內 100元/hours
7hr以內 120元/hours
超過7hr 130元/hours

import java.util.Scanner;
public class HomeWork0703 {

public static void main(String[] args) {

int[]hoursTable ={0,1,2,3,5,7};//0不可以設定成1,否則無法正常計算
int[]feeTable= {30,50,70,100,120,130};

Scanner sc=new Scanner(System.in);
int hours=0;
hours=sc.nextInt();

int i =hoursTable.length-1;
int fee=0;

while(i>0){
if(hoursTable[i]<hours)
break;
i--;
}

while(i>=0){
fee+=(hours-hoursTable[i])*feeTable[i];
hours=hoursTable[i];
i--;
}

System.out.print(fee);
sc.close();
}
}


第七章 10個字串存入10個陣列

題目:使用者輸入10個資料,之後由後往前輸出每筆資料,並用逗號隔開

import java.util.Scanner;
public class HomeWork0702 {

public static void main(String[] argv) {

Scanner kbd = new Scanner(System.in);

System.out.println("請輸入10個字串");

String[] str = new String[10];//建立儲存10個字串的陣列


for(int i = 0; i < str.length; i++){
System.out.println("請輸入第" + (i+1)  + "個字串");
str[i] = kbd.nextLine();
}

for(int i = 0; i < str.length; i++){
System.out.print(str[i] + " ");
}
kbd.close();

}

}


第六章 數學方程式測試

題目:讓使用者輸入x&n,計算出上列的式子


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class HomeWork0610 {

public static void main(String[] args)throws IOException{
BufferedReader br= new
BufferedReader(new InputStreamReader(System.in));
System.out.println("請輸入x ");

double x=java.lang.Double.parseDouble(br.readLine());

System.out.println("請輸入n ");

double n=java.lang.Double.parseDouble(br.readLine());

double sum=0;

for(int i=1;i<=n;i++)
sum+=((x+i)/(n-i+1));
if(n==0)
System.out.print("n不可為零");
else
System.out.print(sum);
}

}

第六章 密碼測試

題目:
讓使用者輸入兩次密碼(四位數字)
給予兩次密碼輸入錯誤的機會
第三次輸入錯誤就給予錯誤的訊息

本人有使用類似作業系統的概念
Monitor? 號誌?

import java.util.Scanner;
public class HomeWork0609 {

public static void main(String[] args) {
 Scanner sc = new Scanner(System.in);
     System.out.println("請輸入四位數密碼");

int key=1234;//正確密碼
boolean a=false;//測試關卡用
int count=0;//錯誤次數

while(a!=true&(3-count)>0){
int s = sc.nextInt();
if(s==key)
{
System.out.print("輸入正確");
a=true;
}

else
{
if((3-count)>1){
System.out.print("輸入錯誤,請重新輸入");
System.out.println("還可再試"+(2-count)+"次");
count++;
}
else
{
System.out.print("錯誤超過三次,系統中止");
count++;
}
}
}
sc.close();
}
}

第六章 菱形星星

public class HomeWork0607 {

public static void main(String[] args) {

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

for(int j=3;j-i>=0;j--){
System.out.print(" ");
}//左上半部空格

for(int k=1;k<=2*i-1;k++)
{
System.out.print("*");
}//中上半部的星號

System.out.println("");
}

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

for(int j=1;j<i+1;j++)
{
System.out.print(" ");
}//印左下半部空白處

for(int k=7;k-i*2>0;k--){
System.out.print("*");
}//印左下半部的星號

System.out.println("");
}
}
}

第六章 上三角形

public class HomeWork06071 {

public static void main(String[] args){
for(int i=0;i<5;i++){
for(int j=1; j-i<2;j++){

System.out.print("*");

}
System.out.print("\n");
}


}

本部落格之聲明

大家好

我是阿亮

目前在台北職能發展學院上Java程式設計班

這個部落格的目的是給我組員使用

在對照答案的時候有個參考

但並不保證所有答案的正確性




本人並不鼓勵在沒有認真寫過該題目

就直接來我的Blog抄我的答案

本部落格是採用

「最新Java2程式設計(SCJP)」2015/02

歡迎大家可以針對我寫的程式提出疑問

謝謝大家