// Filename: JavaPrograms.txt // Programmer: anne.dawson@gmail.com // First created: Sunday 9th June 2007, 8:19 PT // Last updated: Thursday 2nd October 2008, 14:01 PT // Please Note: lines starting with the two characters // are comments // Commnents and are ignored by the Java compiler/interpreter... // See: // http://www.annedawson.net/JavaComments.html // for important comments about comments. // Any of these example programs can be run by // directly copying the desired program and pasting // the code to a Java editor such as JCreator... // http://www.annedawson.net/Java_Program_Run.htm // The first Java program (prog_01_01.java) has only // one executable line: // System.out.println("Hello World!); // A selection of these example programs are used in courses // CSCI102 and CSCI201 /* ********************************************************************************** Please note: all textbook source code from the 4th Ed and 5th Ed textbook can be found here: http://www.annedawson.net/java4th.zip http://www.annedawson.net/java5th.zip "Java - An Introduction to Problem Solving and Programming" by Walter Savitch and Frank M. Carrano 5th Ed ISBN:0-13-607225-9 "Java - An Introduction to Problem Solving and Programming" by Walter Savitch 4th Ed ISBN:0-13-149202-0 ********************************************************************************** */ // prog_01_01.java /** * @(#)prog_01_01.java * * prog_01_01 application * * @author * @version 1.00 2007/9/9 */ public class prog_01_01 { public static void main(String[] args) { System.out.println("Hello World!"); } } // Links to Example Programs from Anne Dawson's powerpoints // in order of presentation: http://www.annedawson.net/EggBasket.java http://www.annedawson.net/SavitchIn.java [Note: SavitchIn.java is needed to run some of the Java program's referred to in the powerpoints - read important comments in EggBasket.java] All powerpoint programs on: http://www.annedawson.net/ "Java - An Introduction to Problem Solving and Programming" by Walter Savitch 4th Ed ISBN:0-13-149202-0 Ch01 - Intro no textbook programs see program prog_01_01.java - see above in this document 5th Ed 4th Ed FirstProgram.java p15 Ch02 - Types, I/O and Doc 5th Ed 4th Ed EggBasket.java p46 p48 EggBasket2.java p53 p57 ChangeMaker.java p70 p72 StringDemo.java p79 p84 ScannerDemo p87 not in earlier editions Documentation and Style p93-98 p98-105 Ch03 - Flow 5th Ed 4th Ed StringEqualityDemo.java p136 p138 Grader.java p145 p147 MultipleBirths.java p156 p150 ForDemo.java p197 p169 BreakDemo.java p176 ExamAverager.java p194 p185 BooleanDemo.java p210 p198 Ch04 - Flow Looping (5th Edition only) WhileDemo.java p179 DoWhileDemo.java p184 ExamAverager.java p194 ForDemo.java p197 BooleanDemo.java p210 ********************************************************************************************************************** // Start of CSCI102A_LAB2B_VER1_FA08.java // Needs SavitchIn.java in the same folder and built first public class CSCI102A_LAB2B_VER1_FA08 { public static void main(String[] args) { char temperatureScale; double temperature; double degreesF = 0.0; double degreesC = 0.0; System.out.println("Enter a temperature value:"); temperature = SavitchIn.readLineDouble(); System.out.println("The temperature you entered is"); System.out.println(temperature); System.out.println("Enter a temperature scale (C or F):"); temperatureScale = SavitchIn.readChar(); System.out.println("The temperature scale you entered is"); System.out.println(temperatureScale); if (temperatureScale == 'C') { degreesF = 9 * temperature / 5 + 32; System.out.println(degreesF + " degrees F"); } else { degreesC = 5 * (temperature - 32 ) /9; System.out.println(degreesC + " degrees C"); } } } // End of CSCI102A_LAB2B_VER1_FA08.java ********************************************************************************************************************** // Start of CSCI102A_LAB2B_VER2_FA08.java // Needs no other file import java.util.Scanner; public class CSCI102A_LAB2B_VER2_FA08 { public static void main(String[] args) { Scanner scannerObject = new Scanner(System.in); char temperatureScale; double temperature; double degreesF = 0.0; double degreesC = 0.0; System.out.println("Enter a temperature value:"); temperature = scannerObject.nextDouble( ); System.out.println("The temperature you entered is"); System.out.println(temperature); System.out.println("Enter a temperature scale (C or F):"); String str; str = scannerObject.next( ); temperatureScale = str.charAt(0); // gets the first character of the entered string // and assigns it to the char variable // the Scanner class does NOT have a method to input a single character System.out.println("The temperature scale you entered is"); System.out.println(temperatureScale); if (temperatureScale == 'C') { degreesF = 9 * temperature / 5 + 32; System.out.println(degreesF + " degrees F"); } else { degreesC = 5 * (temperature - 32 ) /9; System.out.println(degreesC + " degrees C"); } } } // End of CSCI102A_LAB2B_VER2_FA08.java ********************************************************************************************************************** // Start of CSCI102A_LAB3A_VER1_FA08.java // Needs SavitchIn.java in the same folder and built first public class CSCI102A_LAB3A_VER1_FA08 { public static void main(String[] args) { // this program is a copy of: // CSCI102A_LAB2B_VER1_FA08.java // amended so it repeats at the user's request char answer = 'y'; // new char temperatureScale; double temperature; double degreesF = 0.0; double degreesC = 0.0; while (answer == 'y') //new { System.out.println("Enter a temperature value:"); temperature = SavitchIn.readLineDouble(); System.out.println("The temperature you entered is"); System.out.println(temperature); System.out.println("Enter a temperature scale (C or F):"); temperatureScale = SavitchIn.readLineNonwhiteChar( ); // changed System.out.println("The temperature scale you entered is"); System.out.println(temperatureScale); if (temperatureScale == 'C') { degreesF = 9 * temperature / 5 + 32; System.out.println(degreesF + " degrees F"); } else { degreesC = 5 * (temperature - 32 ) /9; System.out.println(degreesC + " degrees C"); } System.out.println("Run again? (y/n):"); // new answer = SavitchIn.readLineNonwhiteChar( ); //new } } } // End of CSCI102A_LAB3A_VER1_FA08.java ********************************************************************************************************************** // Start of CSCI102A_LAB3A_VER2_FA08.java // Needs no other file import java.util.Scanner; public class CSCI102A_LAB3A_VER2_FA08 { public static void main(String[] args) { // this program is a copy of: // CSCI102A_LAB2B_VER2_FA08.java // amended so it repeats at the user's request Scanner scannerObject = new Scanner(System.in); char answer = 'y'; // new char temperatureScale; double temperature; double degreesF = 0.0; double degreesC = 0.0; while (answer == 'y') //new { System.out.println("Enter a temperature value:"); temperature = scannerObject.nextDouble( ); System.out.println("The temperature you entered is"); System.out.println(temperature); System.out.println("Enter a temperature scale (C or F):"); String str; str = scannerObject.next( ); temperatureScale = str.charAt(0); // gets the first character of the entered string // and assigns it to the char variable // the Scanner class does NOT have a method to input a single character System.out.println("The temperature scale you entered is"); System.out.println(temperatureScale); if (temperatureScale == 'C') { degreesF = 9 * temperature / 5 + 32; System.out.println(degreesF + " degrees F"); } else { degreesC = 5 * (temperature - 32 ) /9; System.out.println(degreesC + " degrees C"); } System.out.println("Run again? (y/n):"); // new str = scannerObject.next( ); //new answer = str.charAt(0); // gets the first character of the entered string // and assigns it to the char variable // the Scanner class does NOT have a method to input a single character } } } // End of CSCI102A_LAB3A_VER2_FA08.java ********************************************************************************************************************** // Start of CSCI102A_LAB3B_VER1_PART1_FA08.java // Needs SavitchIn.java in the same folder and built first public class CSCI102A_LAB3B_VER1_PART1_FA08 { public static void main(String[] args) { char answer = 'y'; int number = 0; int total = 0; double average = 0.0; int count = 0; while (answer == 'y') { count = 0; number = 0; total = 0; System.out.println("Please enter a list of whole numbers."); System.out.println("Press the enter key after entering each number."); System.out.println("Signal the end of the list by entering -1:\n\n"); while (number != -1) { number = SavitchIn.readInt(); if (number != -1) { count++; total = total + number; } } // end of innermost while statement System.out.println("You entered " + count + " numbers."); System.out.println("Total is: " + total); average = (double)total / count; // type casting from int to double - p60 5th Ed System.out.println("Average is: " + average ); System.out.println("\n\nRun again? (y/n):"); answer = SavitchIn.readLineNonwhiteChar( ); } // end of outermost while statement System.out.println("\n\nThank you for using this program. Goodbye!"); } } // End of CSCI102A_LAB3B_VER1_PART1_FA08.java ********************************************************************************************************************** // Start of CSCI102A_LAB3B_VER1_PART2_FA08.java // Needs SavitchIn.java in the same folder and built first public class CSCI102A_LAB3B_VER1_PART2_FA08 { public static void main(String[] args) { char answer = 'y'; int number = 0; int total = 0; double average = 0.0; int count = 0; int max = 0; // to store the highest integer - assuming it will be larger than zero int min = 1000000; // to store the smallest number - assume smaller than a million while (answer == 'y') { count = 0; number = 0; total = 0; min = 1000000; max = 0; System.out.println("Please enter a list of whole numbers."); System.out.println("Press the enter key after entering each number."); System.out.println("Signal the end of the list by entering -1:\n\n"); while (number != -1) { number = SavitchIn.readInt(); if (number != -1) { count++; total = total + number; if (number > max) max = number; if (number < min) min = number; } } // end of innermost while statement System.out.println("You entered " + count + " numbers."); System.out.println("Total is: " + total); average = (double)total / count; // type casting from int to double - p60 5th Ed System.out.println("The largest number is: " + max ); System.out.println("The smallest number is: " + min ); System.out.println("Average is: " + average ); System.out.println("\n\nRun again? (y/n):"); answer = SavitchIn.readLineNonwhiteChar( ); } // end of outermost while statement System.out.println("\n\nThank you for using this program. Goodbye!"); } } // End of CSCI102A_LAB3B_VER1_PART2_FA08.java ********************************************************************************************************************** // Start of CSCI102A_LAB3B_VER2_PART1_FA08.java // Needs no other files import java.util.Scanner; public class CSCI102A_LAB3B_VER2_PART1_FA08 { public static void main(String[] args) { char answer = 'y'; int number = 0; int total = 0; double average = 0.0; int count = 0; String str; Scanner scannerObject = new Scanner(System.in); while (answer == 'y') { count = 0; number = 0; total = 0; System.out.println("Please enter a list of whole numbers."); System.out.println("Press the enter key after entering each number."); System.out.println("Signal the end of the list by entering -1:\n\n"); while (number != -1) { number = scannerObject.nextInt( ); if (number != -1) { count++; total = total + number; } } // end of innermost while statement System.out.println("You entered " + count + " numbers."); System.out.println("Total is: " + total); average = (double)total / count; // type casting from int to double - p60 5th Ed System.out.println("Average is: " + average ); System.out.println("\n\nRun again? (y/n):"); str = scannerObject.next( ); answer = str.charAt(0); // gets the first character of the entered string // and assigns it to the char variable // the Scanner class does NOT have a method to input a single character } // end of outermost while statement System.out.println("\n\nThank you for using this program. Goodbye!"); } } // End of CSCI102A_LAB3B_VER2_PART1_FA08.java ********************************************************************************************************************** // Start of CSCI102A_LAB3B_VER2_PART2_FA08.java // Needs no other files import java.util.Scanner; public class CSCI102A_LAB3B_VER2_PART2_FA08 { public static void main(String[] args) { char answer = 'y'; int number = 0; int total = 0; double average = 0.0; int count = 0; int max = 0; // to store the highest integer - assuming it will be larger than zero int min = 1000000; // to store the smallest number - assume smaller than a million String str; Scanner scannerObject = new Scanner(System.in); while (answer == 'y') { count = 0; number = 0; total = 0; min = 1000000; max = 0; System.out.println("Please enter a list of whole numbers."); System.out.println("Press the enter key after entering each number."); System.out.println("Signal the end of the list by entering -1:\n\n"); while (number != -1) { number = scannerObject.nextInt( ); if (number != -1) { count++; total = total + number; if (number > max) max = number; if (number < min) min = number; } } // end of innermost while statement System.out.println("You entered " + count + " numbers."); System.out.println("Total is: " + total); average = (double)total / count; // type casting from int to double - p60 5th Ed System.out.println("The largest number is: " + max ); System.out.println("The smallest number is: " + min ); System.out.println("Average is: " + average ); System.out.println("\n\nRun again? (y/n):"); str = scannerObject.next( ); answer = str.charAt(0); // gets the first character of the entered string // and assigns it to the char variable // the Scanner class does NOT have a method to input a single character } // end of outermost while statement System.out.println("\n\nThank you for using this program. Goodbye!"); } } // End of CSCI102A_LAB3B_VER2_PART2_FA08.java ********************************************************************************************************************** Ch04 (4th) or Ch05 (5th)- Classes, Objects and Methods 5th Ed 4th Ed 3rd 2nd 1st SavitchIn.java p1013 SpeciesFirstTry.java p246 p225 p185 p214 p158 SpeciesFirstTryDemo.java p247 p227 p187 p217 p159 SpeciesSecondTry.java p264 p244 p204 p234 p177 SpeciesSecondTryDemo.java p265 p245 p205 p235 p178 SpeciesFourthTry.java p277 p258 p218 p249 p192 SpeciesFourthTryDemo.java p278 p259 p219 p250 p193 Species.java p302/5 p277 p237 p272 p210 SpeciesEqualsDemo.java p304 p278 p238 p273 p211 Ch05 - More on Objects and Methods 5th Ed 4th Ed 3rd 2nd 1st SavitchIn.java p1013 Oracle.java p318 OracleDemo.java p320 CircleFirstTry.java p326 CircleDemo.java p327 Circle.java p336 Statistician.java p353 PetRecord.java on www.annedawson.net PetRecordDemo.java on www.annedawson.net Ch06 - Arrays 5th Ed 4th Ed 3rd 2nd 1st SavitchIn.java p1013 ArrayOfTemperatures.java p421 ArrayOfTemperatures2.java p425 ArgumentDemo.java p439 SelectionSort.java p463 Ch07 - Inheritance 5th Ed 4th Ed 3rd 2nd 1st SavitchIn.java p1013 Person.java p503 Student.java p505 InheritanceDemo.java p507 Undergraduate.java p517 Ch08 - Exceptions 5th Ed 4th Ed 3rd 2nd 1st SavitchIn.java p1013 GotMilk.java p572 ExceptionDemo.java p573 DivideByZeroException.java p586 DivideByZeroExceptionDemo.java p587 Ch09 - Streams and File I/O 5th Ed 4th Ed 3rd 2nd 1st SavitchIn.java p1013 TextFileOutputDemo.java p646 TextFileInputDemo.java p657 TextFileInputDemo2.java p661 Ch10 - Data Structures 5th Ed 4th Ed 3rd 2nd 1st SavitchIn.java p1013 StudentNode.java not in textbook, go to: http://www.annedawson.net/StudentNode.java StudentNodeLinkedList.java not in textbook, go to: http://www.annedawson.net/StudentNodeLinkedList.java StudentNodeLinkedListDemo.java not in textbook, go to: http://www.annedawson.net/StudentNodeLinkedListDemo.java ListNode.java p735 StringLinkedList.java p737 LinkedListDemo.java p742 StringLinkedListSelfcontained.java p747