Presentation is loading. Please wait.

Presentation is loading. Please wait.

Examples of class: Using System.in.read ()

Similar presentations


Presentation on theme: "Examples of class: Using System.in.read ()"— Presentation transcript:

1 Examples of class: Using System.in.read ()
Instructor: Mainak Chaudhuri

2 Building a wrapper public class MyInput { // Read a character
public char ReadChar () throws java.io.IOException { return (char)System.in.read (); } // Read multiple characters public String ReadString (int howmany) throws java.io.IOException { String str = “”; int k; for (k=0; k<howmany; k++) { str += (char)System.in.read (); return str;

3 Building a wrapper // Read a line
public String ReadLine () throws java.io.IOException { String str = “”; char c; while ((c=(char)System.in.read ()) != ‘\n’) { str += c; } return str;

4 Building a wrapper // Read an integer
public int ReadInt () throws java.io.IOException { String str = “”; char c; while (true) { c = (char)System.in.read (); if ((c == ‘\n’) || (c == ‘ ‘)) { break; } str += c; Integer n = new Integer (str.trim()); return n.intValue();

5 Building a wrapper // Read a double
public double ReadDouble () throws java.io.IOException { String str = “”; char c; while (true) { c = (char)System.in.read (); if ((c == ‘\n’) || (c == ‘ ‘)) { break; } str += c; Double n = new Double (str.trim()); return n.doubleValue();

6 Building a wrapper // Read a float
public float ReadFloat () java.io.IOException { String str = “”; char c; while (true) { c = (char)System.in.read (); if ((c == ‘\n’) || (c == ‘ ‘)) { break; } str += c; Float n = new Float (str.trim()); return n.floatValue(); } // end class

7 Using the wrapper Let us build an interactive calculator
Asks for two numbers (integers, floats, or doubles) Asks for the operation type: +, -, /, * Computes the answer, prints it, and prompts for the next input User should be asked before closing the calculator I leave it to you to extend it further with other complex operations

8 Using the wrapper class DeskCalculator {
public static void main (String a[]) throws java.io.IOException { char inputOp, exitChar, sundries; MyInput inp = new MyInput(); double operand1, operand2; while (true) { System.out.println (“Enter two numbers:”); operand1 = inp.ReadDouble (); operand2 = inp.ReadDouble (); System.out.print (“What do you want to do? (+, -, *, /)”); inputOp = inp.ReadChar (); // next slide

9 Using the wrapper // Eat up the \n at the end
sundries = inp.ReadChar (); switch (inputOp) { case ‘+’ : System.out.println (operand1+operand2); break; case ‘-’ : System.out.println (operand1-operand2); // next slide

10 Using the wrapper case ‘*’ : System.out.println(operand1*operand2);
break; case ‘/’ : System.out.println(operand1/operand2); default : System.out.println (“Invalid operation!”); } // end switch

11 Using the wrapper System.out.print (“Want to exit? (y/n)”);
exitChar = inp.ReadChar (); // Eat up the ‘\n’ sundries = inp.ReadChar (); if (exitChar == ‘y’) { System.out.println (“Bye for now!”); break; } } // end while } // end main } // end class

12 Announcements Lab today for Thursday batch (B1/B2)
Notes on classes and objects in CopyPoint Extra class on 12th November 1400 to 1615 in L7 Project progress report due on 1st November after class Two pages A convincing executive summary of progress (one page) Future plans with clearly defined deadlines (one page)


Download ppt "Examples of class: Using System.in.read ()"

Similar presentations


Ads by Google