Github 備份:LINK
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* TQC+ JAVA6 - 609_1 */ | |
//建立BoundedStack的類別 | |
class BoundedStack{ | |
String[] bs; | |
int top=-1; | |
int max; | |
//初始化建構子 | |
BoundedStack(int i){ | |
//設定陣列大小 | |
bs = new String[i]; | |
max = i; | |
} | |
//建立一個加元素到陣列的方法 | |
void push(String s){ | |
if(top<max-1) | |
bs[++top]=s;//先++將陣列一開始從-1變成0 | |
else | |
System.out.println("stack-overflow"); | |
} | |
//建立一個將元素從陣列中拋出(需要return值) | |
String pop(){ | |
String s ; | |
if(top>=0) | |
s = bs[top--];//先將該位置的值拋出,再將index-- | |
else | |
s = "stack-is-empty"; | |
return s; | |
} | |
//建立一個方法判斷該陣列是否為空 | |
boolean empty(){ | |
return top == -1; | |
} | |
} | |
public class JPA06_1 { | |
public static void main(String args[]) { | |
//設定陣列大小 | |
BoundedStack s = new BoundedStack(3); | |
//將元素加入到陣列中 | |
s.push("abc"); | |
s.push("def"); | |
s.push("ghi"); | |
//最後一個元素,因陣列大小不構,而無法寫入 | |
s.push("jkl"); | |
//拋出時,先進的會後拋出 | |
System.out.println(s.pop()); | |
System.out.println(s.pop()); | |
System.out.println(s.pop()); | |
//判斷陣列是否為空的 | |
System.out.println(s.empty()); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* TQC+ JAVA6 - 609_2 */ | |
import java.util.LinkedList; | |
//建立一個UnboundedStack的類別 | |
class UnboundedStack{ | |
LinkedList ubs; | |
//建構子初始化為LinkedList | |
UnboundedStack(){ | |
ubs = new LinkedList(); | |
} | |
//建立判斷方法,確定該陣列是否為空 | |
boolean empty(){ | |
return ubs.size() == 0; | |
} | |
//建立將元素放入陣列的方法 | |
void push(String s){ | |
//主要是透過內建的函數來控制 | |
ubs.addFirst(s); | |
} | |
//建立將元素拋出的方法 | |
String pop(){ | |
if(!empty()){ | |
//取得第一個元素後,將第一個元素值移除 | |
String s = (String)ubs.getFirst(); | |
ubs.removeFirst(); | |
return s; | |
} else { | |
return "Stack is empty!!"; | |
} | |
} | |
} | |
public class JPA06_2 { | |
public static void main(String args[]) { | |
UnboundedStack s = new UnboundedStack(); | |
s.push("abc"); | |
s.push("def"); | |
s.push("ghi"); | |
s.push("jkl"); | |
System.out.println(s.pop()); | |
System.out.println(s.pop()); | |
System.out.println(s.pop()); | |
//檢查陣列是否為空的,因仍有一個元素在陣列中,回傳false | |
System.out.println(s.empty()); | |
System.out.println(s.pop()); | |
System.out.println(s.empty()); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* TQC+ JAVA6 - 609_3 */ | |
/* | |
* 該題主要是整合前兩題,前兩題的區別在於 | |
* 第一題:使用陣列,讓使用者自行去控制陣列的index值,決定拋出與堆入的功能 | |
* 第二題:使用到陣列的方法功能讓使用者能呼叫函式去控制資料的進出 | |
*/ | |
import java.util.LinkedList; | |
//建立一個抽象的stack類別 | |
abstract class stack{ | |
stack(){} | |
//建立抽象的方法,待子類別來實作 | |
public abstract String pop(); | |
public abstract void push(String s); | |
//建立一個方法,可以還傳 | |
public String top(){ | |
String s = pop(); | |
push(s); | |
return s; | |
} | |
} | |
class BoundedStack extends stack{ | |
String[] bs; | |
int top=-1; | |
int max; | |
BoundedStack(int i){ | |
bs = new String[i]; | |
max = i; | |
} | |
public void push(String s){ | |
if(top<max-1) | |
bs[++top]=s; | |
else | |
System.out.println("stack-overflow"); | |
} | |
public String pop(){ | |
String s ; | |
if(top>=0) | |
s = bs[top--]; | |
else | |
s = "stack-is-empty"; | |
return s; | |
} | |
boolean empty(){ | |
return top == -1; | |
} | |
} | |
class UnboundedStack extends stack{ | |
LinkedList ubs; | |
UnboundedStack(){ | |
ubs = new LinkedList(); | |
} | |
boolean empty(){ | |
return ubs.size() == 0; | |
} | |
public void push(String s){ | |
ubs.addFirst(s); | |
System.out.println("Pushing:"+s); | |
} | |
public String pop(){ | |
if(!empty()){ | |
String s = (String)ubs.getFirst(); | |
ubs.removeFirst(); | |
System.out.println("Poping:"+s); | |
return s; | |
} else { | |
return "Stack is empty!!"; | |
} | |
} | |
} | |
//繼承UnboundedStack,新增方法取得陣列大小 | |
class TraceUnboundedStack extends UnboundedStack{ | |
int getSize(){ | |
return ubs.size(); | |
} | |
} | |
public class JPA06_3 { | |
public static void main(String args[]) { | |
TraceUnboundedStack s2 = new TraceUnboundedStack(); | |
s2.push("abc"); | |
s2.push("def"); | |
s2.push("ghi"); | |
s2.push("jkl"); | |
//取得陣列大小 | |
System.out.println(s2.getSize()); | |
//進行top方法(先拋出後堆回) | |
System.out.println(s2.top()); | |
//持續拋出 | |
System.out.println(s2.pop()); | |
System.out.println(s2.pop()); | |
System.out.println(s2.pop()); | |
System.out.println(s2.empty()); | |
System.out.println(s2.pop()); | |
System.out.println(s2.empty()); | |
System.out.println(s2.getSize()); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* TQC+ JAVA6 - 609_4 */ | |
import java.util.LinkedList; | |
//這題主要是將先前回傳值為String的部分,改變成Object | |
abstract class stack | |
{ | |
stack(){} | |
public abstract Object pop(); | |
public abstract void push(Object s); | |
public Object top(){ | |
Object s = pop(); | |
push(s); | |
return s; | |
} | |
} | |
class UnboundedStack extends stack{ | |
LinkedList ubs; | |
UnboundedStack(){ | |
ubs = new LinkedList(); | |
} | |
boolean empty(){ | |
return ubs.size() == 0; | |
} | |
public void push(Object s){ | |
ubs.addFirst(s); | |
} | |
public Object pop(){ | |
if(!empty()){ | |
Object s = ubs.getFirst(); | |
ubs.removeFirst(); | |
return s; | |
} else { | |
return null; | |
} | |
} | |
} | |
public class JPA06_4 { | |
public static void main(String args[]) { | |
UnboundedStack s = new UnboundedStack(); | |
s.push("abc"); | |
s.push(2); | |
s.push("ghi"); | |
System.out.println(s.top()); | |
System.out.println(s.pop()); | |
System.out.println(s.pop()); | |
System.out.println(s.pop()); | |
System.out.println(s.pop()); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* TQC+ JAVA6 - 609_5 */ | |
import java.util.LinkedList; | |
abstract class stack{ | |
stack(){} | |
//丟出錯誤時,不要丟出Exception,而是丟出自行撰寫的錯誤類別名稱 | |
public abstract Object pop() throws exnull; | |
public abstract void push(Object s); | |
//新增拋出錯誤的功能 | |
public Object top() throws exnull{ | |
Object s = pop(); | |
push(s); | |
return s; | |
} | |
} | |
class UnboundedStack extends stack{ | |
LinkedList ubs; | |
UnboundedStack(){ | |
ubs = new LinkedList(); | |
} | |
boolean empty(){ | |
return ubs.size() == 0; | |
} | |
public void push(Object s){ | |
ubs.addFirst(s); | |
} | |
public Object pop() throws exnull{ | |
if(!empty()){ | |
Object s = ubs.getFirst(); | |
ubs.removeFirst(); | |
return s; | |
} else { | |
//前幾題在這邊會直接print出來錯誤訊息,這邊則是將資訊包在exception拋出來 | |
throw new exnull("Stack is empty!!"); | |
} | |
} | |
} | |
//新增一個類別,繼承Exception | |
class exnull extends Exception{ | |
exnull(String s){ | |
System.out.println(s); | |
} | |
} | |
public class JPA06_5 { | |
public static void main(String args[]) { | |
try { | |
UnboundedStack s = new UnboundedStack(); | |
s.push("abc"); | |
s.push(2); | |
s.push("ghi"); | |
System.out.println(s.top()); | |
System.out.println(s.pop()); | |
System.out.println(s.pop()); | |
System.out.println(s.pop()); | |
System.out.println(s.pop()); | |
//抓取錯誤時,使用自行撰寫的exception方法 | |
}catch(exnull e){ | |
} | |
} | |
} |
TQC+ JAVA6 試題總覽:LINK
Github 備份:LINK
本篇教學的程式碼皆由筆者編輯,歡迎轉貼本教學,但請全文轉貼,謝啦~
沒有留言:
張貼留言