CS 111 Sample  Programs  Cynthia D. Tanner 

Program Listing      LibraryItem.java .................................. 33  ListDyn.java ........................................... 66  Maze.java ..................................................... 9  Meat.java ................................................... 32  OrderedList.java .................................. 51  Postfix.java ........................................... 44  PostfixA.java ......................................... 58  Problem.java ........................................... 25  Queens.java .............................................. 12  QueueDyn.java ......................................... 63  ReferenceBook.java ............................. 36  RevString.java ......................................... 6  Sets.java ................................................... 52  StackArray.java .................................... 56  StackDyn.java ......................................... 59  Sum.java ....................................................... 3  TestListDyn.java .................................. 68  TestQueueDyn.java ............................... 65  TestStackDyn.java ............................... 61  Tree.java ................................................... 69  TryList.java ........................................... 42  TSimpleList.java .................................. 41  UnOrderedList.java ............................. 50  UseCard.java ........................................... 22  UseDate.java ........................................... 18  UseFood.java ........................................... 32  Veggie.java .............................................. 32 

AllSets.java ............................................ 54  Balance.java ............................................ 43  Balance2.java ......................................... 48  BalanceA.java ......................................... 57  BankQueue.java ....................................... 45  BinarySearch.java .................................. 8  Blackjack.java ....................................... 23  Book.java ................................................... 35  Card.java ................................................... 20  CD.java ........................................................ 37  Date.java ................................................... 14  Disc.java ................................................... 36  DisplayQueue.java ................................ 46  DVD.java ..................................................... 38  Exceptions.java .................................... 27  Exceptions2.java .................................. 28  Exceptions3.java .................................. 29  Exceptions3a.java ................................ 29  Exceptions3b.java ................................ 30  Exceptions4.java .................................. 47  Fib.java ....................................................... 6  FibIt.java .................................................. 7  Flash.java ................................................ 26  Food.java ................................................... 31  Gcd.java ....................................................... 3  Knights ........................................................ 11  Library.java ............................................ 39   

  2   

--- Begin Sum.java --1: //Sum.java 2: //solution to page 165 #8 3: 4: import java.util.Scanner; 5: 6: public class Sum{ 7: public static int sum (int n) 8: { 9: System.out.println("On this call n = " + n); 10: if (n==1) 11: return 1; 12: else return (n + sum(n-1)); 13: System.out.println("On return n = " + n); E: unreachable statement E: System.out.println("On return n = " + n); 14: E: E:

} missing return statement }

15: 16: public static void main(String[] args){ 17: int value; 18: Scanner valueIn = new Scanner(System.in); 19: 20: System.out.print("enter the value to sum to: "); 21: value = valueIn.nextInt(); 22: 23: System.out.println("the sum of the number 1-" + value + " is: " + sum(value)); 24: } 25: } --- End Sum.java ---

--- Begin Gcd.java --1: //Gcd.java 2: //CS 111 sample program to illustrate calculating the greatest common divisor 3: 4: /**public static int Gcd (int a, int b) 5: * pre condition ba 11: * error terminate if a or b = 0 12: */ 13:

3   

14: /* test plan 15: goal input 16: valid 26, 8 > 1 17: boundary 125, 25 18: valid 1024, 36 > 2 19: boundary 36, 36 20: boundary 735, 16 factors other than 1 21: valid 24, -20 < 0 22: invalid 8, 26 23: invalid 4, 0 24: invalid 0,4 25: */ 26: 27: import java.util.Scanner; 28: 29: public class Gcd 30: { 31: 32: public static int gcdr(int a, int b) 33: { //precondition b=0 && r < BOARDSIZE && c >=0 && c < BOARDSIZE); 14: } 15: 16: private static boolean available(int r, int c) 17: { 18: return (board[r][c]== -1); 19: } 20: 21: private static boolean ktour (int r, int c, int moveNum) 22: { 23: int i, nr=0, nc=0; 24: 25: //System.out.println("movenum=" + moveNum); 26: //is spot acceptable 27: if (!onBoard(r,c) || !available(r,c)) return false; 28: 29: //record move 30: board[r][c]=moveNum; 31: 32: //done the whole board? 33: if (moveNum==BOARDSIZE*BOARDSIZE) return true; 34: 35: //pick next move 36: for (i=0;is, face=>f 37: { suit =s; 38: face = f; 39: } 40: 41: //accessors 42: public Suits suit () 43: {return suit;} // suit of the card 44: 45: public int face () 46: {return face;} // returns face value of card 47: 48: //Function to display card 49: public void printcard() 50: { 51: System.out.print("The card is "); 52: if (face > 1 && face =1 && number