1 / 18

Simple Programs

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

martha
Download Presentation

Simple Programs

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Simple Programs Simple Strings Writing some programs Only in Parameters? I/O Gadget

  2. 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 */

  3. 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

  4. 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!

  5. 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!

  6. Average some numbers 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 Recall: Typing java Average will cause Java to search in class Average for method main

  7. 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

  8. 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 !!!!!!

  9. 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 \

  10. 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!

  11. 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!

  12. 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

  13. 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

  14. 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

  15. 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

  16. Calculate a square root import java.math.*; class SqrtTest { public final static double epsilon = .0000001; 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

  17. 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

More Related