1 / 39

Some basic I/O

Some basic I/O. Example of Output Stream. Our first program involved the output stream // First Program HelloWorld public class HelloWorld { public static void main(String args []){ System.out.println(“Hello world...”): } }. System.out.println(“Hello world...”):.

orea
Download Presentation

Some basic I/O

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. Some basic I/O

  2. Example of Output Stream • Our first program involved the output stream • // First Program HelloWorld • public class HelloWorld { • public static void main(String args []){ • System.out.println(“Hello world...”): }}

  3. System.out.println(“Hello world...”): • System.out is called the standard output object • This will display a line of text in the command window • In Java , any source and destination for I/O is considered a stream of bytes or characters. • To perform output we insert bytes or characters into a stream. To perform input we extract bytes or characters from a stream. • java.lang.System class contains three predefined streams • System.out • System.err for errors • System.in

  4. Consider • System.in

  5. System.in • Standard input is this stream • Some books refer to the input method • System.in.readln();

  6. Problem • System.in.readln() doesn’t seem to work with system here • Solution import the scanner class

  7. Consider the following program • // Second Program HelloWorld • import java.util.Scanner; • public class HelloWorld3 { • public static void main(String args []){ • Scanner input = new Scanner( System.in ); • String theName = input.nextLine(); • System.out.println(“input was“ + theNAme);}}

  8. It contains the lines • // Second Program HelloWorld • import java.util.Scanner; • This imports an input Utility class called Scanner • Scanner input = new Scanner( System.in ); • This creates an instance called input of the scanner utility

  9. String theName = input.nextLine(); • This calls a method of the class called nextLine() • This gets input from the screen • It binds this to a variable called theName which is declared to be a string type by the String Keyword

  10. So now we have a basic input method which seems to work • Next we need to consider the basic components of programs in order to start building more complex examples. • We will start with basic syntax and operators

  11. Java Syntax Primitive data types Operators Control statements

  12. Primitive data types • Identical across all computer platforms  portable

  13. Primitive data types • char (16 bits) a Unicode character • byte (8 bits) • int (32 bits) a signed integer • short (16 bits) a short integer • long (64 bits) a long integer

  14. Primitive data types • float (32 bits) a real number • double (64 bits) a large real number • boolean (8 bits) • values are true or false(keywords) • cannot be converted to and from other data typese.g.while (i!=0) not while(i)

  15. Primitive Data Types

  16. Operators • Addition Subtraction • + - • Increment operators (postfix and prefix)++ -- • Multiplication Division Modulus* / % • Equality== != • Assignment operators= += -= *= /= %=

  17. Arithmetic Operators

  18. Back to our simple input program • Remember the last night we had a program to input an integer, manipulate it and output it.

  19. We used • input.nextLine(); Returns a String • It would need to be converted if we are to use this string as an integer using the following parseInt method

  20. Parsing Strings for integers • Integer.parseInt(String s); • The parseInt() function parses a string and returns an integer.

  21. Using our Scanner input method • // Second Program HelloWorld • import java.util.Scanner; • public class HelloWorld4 { • public static void main(String args []){ • Scanner input = new Scanner( System.in ); • String theName = input.nextLine(); • int num1 = Integer.parseInt(theName); • num1 = num1 + 1; • System.out.println("result" + num1);}}

  22. Some anomalies • Note the String declaration starts with an Uppercase Letter • And the int declaration starts with a lowercase letter

  23. String Contatenation anomaly • Note we use the arithmetic operator + to increment num1 by 1 • We use the same notation + to concatenate the result num1 to the string result an example of operator overloading which in turn is part of polymorphism

  24. Use of parseInt • int num1 = Integer.parseInt(theName); • We use the method parseInt on the string theName to produce our integer num1

  25. Exercise • Write code to input 2 integers , add them together and output the result.

  26. Towards more complicated programming • Next Conditionals and Loops • Now all Conditionals and Loops are characterised by Conditions (called guards in the case of Loops) • Conditions are boolean expressions largely based on relational operators • Next we look at these operators

  27. Operators • Relational operators< <= > >= • Conditional operator?:

  28. Logical Operators • Not ! • Logical AND && • Logical OR || • Boolean logical AND & • Boolean logical inclusive OR | • Boolean logical exclusive OR ^(true if only one operand is true)

  29. Relational Operators

  30. If Statements • Similarto C/C++ syntax: • if statement if (x != n){ …}else if { … }else { … }

  31. Example of Classic Conditional Program • Consider our Leap Year program

  32. // Second Program HelloWorld • import java.util.Scanner; • public class Leapyear { • public static void main(String args []){ • Scanner input = new Scanner( System.in ); • String theName = input.nextLine(); • int num1 = Integer.parseInt(theName); • if(num1 % 4 ==0 && (num1 % 100 != 0 ||num1 % 400 == 0)) • System.out.println("leap"); • else • System.out.println("not leap"); • }}

  33. Exercises • Write javaclasses which will • 1: Input two Integers and output the largest • 2 :Input 3 sides of a triangle and determine whether the triangle is scalene, isoceles or equilateral. • 3 Input a month and a year and calculate how many days are in that month

  34. Exercises continued • Remember • 30 days hath September , April , June and November, • All the Rest have 31, • Except February which has 28, • save for one year in four • When it has one day more

  35. More Exercises • An insurance company offers different classes of insurance based on age • Over 20 and less than 40 is Category A • Between 40 and 60 is Category B • Over 60 is Category C • Write a program to accept in someones age and tell them the class of insurance they are entitled to.

  36. Control Statements • switch statement switch (n){ case 1: … break; case 2: case 3: … break; default: break;};

  37. Loop Statements • for statement for (int i=0; i<max; i++){ … }; • while statement while (x==n ){ …}; • do statement do {…} while( x<=y && x!=0);

  38. General Points to Note • Case sensitive • Use lower case • objects have capitalised first letter

  39. Reserved Words

More Related