// Filename: JavaPrograms.txt // Programmer: anne.dawson@gmail.com // First created: Sunday 9th June 2007, 8:19 PT // Last updated: Sunday 28th August 2011, 9:14 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 the course // CSCI125: http://www.annedawson.net/Java_Program_Run.htm /* ********************************************************************************** "Java - An Introduction to Problem Solving and Programming" by Walter Savitch 6th Ed ISBN-13: 9780132162708 "Java - An Introduction to Problem Solving and Programming" by Walter Savitch and Frank M. Carrano 5th Ed ISBN:0-13-607225-9 ********************************************************************************** */ // 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!"); } } ********************************************************************************************************************** ********************************************************************************************************************* // 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_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_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 **********************************************************************************************************************