simple programs
Download
Skip this Video
Download Presentation
Simple Programs

Loading in 2 Seconds...

play fullscreen
1 / 18

Simple Programs - PowerPoint PPT Presentation


  • 71 Views
  • Uploaded on

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

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about ' Simple Programs' - martha


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.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
simple programs

Simple Programs

Simple Strings

Writing some programs

Only in Parameters?

I/O Gadget

comments
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
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
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
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!
average some numbers
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

average using a function
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

java only has in parameters
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
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
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
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
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
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

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

and factorial
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

ad