package javareview; import java.io.*; import java.util.Scanner; import javax.swing.JOptionPane; /** * * @author Mayelin */ public class JavaReview2 { public static void main(String[] args) throws FileNotFoundException, IOException { /****************************/ /* Chapter 4 */ /****************************/ // example of a while loop int n = 1729; int sum = 0; int digit; while (n > 0) { digit = n % 10; sum += digit; n = n / 10; } System.out.println("The sum of the digits is: " + sum); // another example of a while loop String input; int number; input = JOptionPane.showInputDialog("Enter a number in the range of 1 through 100."); number = Integer.parseInt(input); // validate the input while( number < 1 || number > 100 ) { input = JOptionPane.showInputDialog("Enter a number in the range of 1 through 100."); number = Integer.parseInt(input); } System.out.println( number + " is a valid value."); // example of a do-while loop do { input = JOptionPane.showInputDialog("Enter a number in the range of 1 through 100."); number = Integer.parseInt(input); } while( number < 1 || number > 100 ); System.out.println( number + " is a valid value."); // nested for loop System.out.println("Enter a width"); Scanner keyboardInput = new Scanner( System.in ); int width = keyboardInput.nextInt(); String output = ""; for (int i = 1; i <= width; i++) { // Make triangle row for (int j = 1; j <= i; j++) output += "[]"; output += "\n"; } System.out.println(output); // reading and writing to files readFromFile(); writeToFile(); appendToFile(); /****************************/ /* Chapter 5 */ /****************************/ // passing arguments to a method int x = 10; displayValue(5); // Pass 5 into num displayValue(x); // Pass 10 into num displayValue(x * 4); // Pass 40 into num displayValue(Integer.parseInt("700")); // Pass 700 into num int myNumber = 6; // using the value returned by a method call if( isValidNumber(myNumber) ) System.out.println("\n" + myNumber + " is a valid value."); else System.out.println("\n" + myNumber + " is an invalid value."); // hierarchical method call System.out.println("\nI am starting in main"); deep(); System.out.println("Now I am back in main"); // passing primitive values to a method int numberOne = 3; int numberTwo = 7; System.out.println("\nThe sum of numbers is: " + addNumbers(numberOne, numberTwo) ); System.out.println("The value of numberOne is " + numberOne); // passing an object reference to a method Student student = new Student("John"); System.out.println("\nThe student name is " + student.getName()); changeStudentName(student); System.out.println("The student name is " + student.getName() + "\n"); // the arguments' data type must match the parameters' data type // the displayValue method expects an int value short s = 1; displayValue(s); // Java converts short to int double d = 1.0; displayValue( (int)d ); // Java won't convert a double to an int unless you cast // See the "Conversion between Primitive Data Types" powerpoint slide System.exit(0); } public static void readFromFile() throws FileNotFoundException { // Open the file for reading // you need to change this to a path in your local machine File file = new File("/Users/Maye/Documents/Teaching/COP3804/Input_Output_Files/file_test.txt"); // this line may throw an exception Scanner inputFile = new Scanner( file ); // read all values from the file and print them to the console window while( inputFile.hasNextLine() ) { System.out.println( inputFile.nextLine() ); } inputFile.close(); } public static void writeToFile() throws FileNotFoundException { // Open the file for writing File file = new File("/Users/Maye/Documents/Teaching/COP3804/Input_Output_Files/output.txt"); // this line may throw an exception PrintWriter outputFile = new PrintWriter(file); outputFile.println("This"); outputFile.println("is"); outputFile.println("the latest"); outputFile.println("test."); outputFile.close(); } public static void appendToFile() throws IOException { // Open the file for writing // this line may throw an exception FileWriter writer = new FileWriter("/Users/Maye/Documents/Teaching/COP3804/Input_Output_Files/output.txt", true); PrintWriter outputFile = new PrintWriter(writer); outputFile.println("APPENDING"); outputFile.println("This"); outputFile.println("is"); outputFile.println("another"); outputFile.println("test."); outputFile.close(); } public static void displayValue(int num) { System.out.println("The value is " + num); } public static boolean isValidNumber(int x) { if( x >= 0 && x <= 10 ) return true; else return false; // the body of this method could just be written as: //return x >= 0 && x <= 10; } public static void deep() { System.out.println("I am now in deep"); deeper(); System.out.println("Now I am back in deep"); } public static void deeper() { System.out.println("I am now in deeper"); } public static double addNumbers(int x, int y) { double sum = x+y; x = 9; return sum; } public static void changeStudentName(Student studentObject) { studentObject.setName("Bob"); } }