Simple Programs Simple Strings Writing some programs Only in Parameters? I/O Gadget
Comments As noted Java accepts comments which look like those we used in pseudocode // This is a comment // Each line must start with the double // slashes Java also understands multi-line comments They start with /* and end with */ /* So this would be just fine */
Jumping Ahead To make life easier we’re going to start using a type of object that comes with Java: A String We’ll give you a simplified version for now and simply say that it will be a little more complicated than Pseudocode Strings
Simple Strings Creating a String variable* String s1; s1 = "My first String"; String s2 = "Hi there!"; *Warning: If you have experience with Basic, C or many other languages, Java Strings don’t work the same!
String Stuff Strings can be concatenated "Hello " + "World" + "!" Java will make a lot of simple conversions for us: System.out.println("Value of x is " + x); More later!
class Average { public static void main(String args[]) { double x = 27.33; double y = 37.98; double z = 12.00; System.out.println((x + y + z)/3.); } // main } // Average Average some numbers Recall: Typing java Average will cause Java to search in class Average for method main
Average using a Function class Average { public static double average (double a, double b, double c) { return (a + b + c)/3.; } // average public static void main(String args[]) { double x = 27.33; double y = 37.98; double z = 12.00; System.out.println(average(x, y, z)); } // main } // Average
in Java only has in parameters! class SwapDemo { public static void swap(int x, int y) { int t; t = x; x = y; y = t; System.out.println("Swap x = "+ x +" y = " + y); } // swap public static void main(String args[]) { int a = 33; int b = 56; swap(a, b); System.out.println("Swap a = "+ a +" b = " + b); } // main } // SwapDemo Swap x = 56 y = 33 Swap a = 33 b = 56 !!!!!!
Print some text class PrintDemo { public static void main(String args[]) { System.out.println("If lines are too long " + "They can be broken up with the + sign " + "In addition special characters can be\n" + "inserted. They are always prefaced by " + "the backslash character \\"); } If lines are too long They can be broken up with the + sign In addition special characters can be inserted. They are always prefaced by the backslash character \
Data Input Java has very poor console (DOS Screen) input facilities Why? Most real Java applications use Graphical User Interfaces (GUIs) Thus Input typically occurs in some kind of text box or other Widget!
Instructions Cut and paste the following slides into files called: Stub.java IOGadget.java The file names must be exactly as shown Put both of files in a separate directory –i.e. Both files in the same separate directory! Compile with javac *.java Test with java Stub Now you have a gadget that will let you read in strings, doubles and ints with built-in prompting!
Tester class Stub { public static void main(String args[]) { String s; double d; int i; s = IOGadget.readLine("Enter a string"); d = IOGadget.readDouble("Enter a double"); i = IOGadget.readInt("Enter an int"); System.out.println ("Just read in string: " + s + " double: " + d + " int: " + i); } // main } // Stub
IOGadget import java.io.*; public class IOGadget { private IOGadget(){} private static InputStreamReader isr = new InputStreamReader(System.in); private static BufferedReader br = new BufferedReader(isr); public static final String readLine(String p) { String retVal = ""; System.out.print(p+"> "); try {retVal = br.readLine(); } catch (Exception e) {System.err.println("IOGadget: " + e.getMessage());} return retVal; } // readLine
IOGadget public static int readInt(String prompt) { try { return Integer.parseInt(readLine(prompt)); } catch(Exception e) { System.out.println("Error reading int"); return 0; } public static double readDouble(String prompt) { try { return Double.parseDouble(readLine(prompt)); } catch(Exception e) { System.out.println ("Error reading double"); return 0.0; } } // IOGadget
Read in a number and make a calculation class NumReader { public static void main(String args[]) { double x, y; x = IOGadget.readDouble ("Enter a number"); y = 2. * x; System.out.println("x = " + x + " 2x = " + y); } // main } // NumReader
Calculate a square root import java.math.*; class SqrtTest { public final static double epsilon = ; public static double sqrt(double d) { double answer = 2.; while(Math.abs(answer*answer - d) > epsilon) { double guess = d/answer; answer = (guess + answer)/2.; } // while return answer; } // sqrt public static void main(String args[]) { double number; number = IOGadget.readDouble("Enter a double"); System.out.println("Sqrt is " + sqrt(number)); } // main } // Sqrttest
And factorial??? class FactDemo { public static int fact(int n) { if(n == 0) return 1; else return n * fact(n - 1); } // fact public static void main(String args[]) { int number; number = IOGadget.readInt ("Enter a non-negative int"); System.out.println("Factorial " + fact(number)); } // main } // FactDemo