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

Slides:



Advertisements
Similar presentations
I/O Basics 12 January 2014Smitha N. Pai, CSE Dept.1.
Advertisements

Basic Java Constructs and Data Types – Nuts and Bolts
Taking Input Java Md. Eftakhairul Islam
Introduction to Programming Java Lab 1: My First Program 11 January JavaLab1.ppt Ping Brennan
DONT PANIC!! Lots of new notions coming in these slides Dont worry if not all of it makes perfect sense Well meet most of this stuff again in detail later.
Input review If review Common Errors in Selection Statement Logical Operators switch Statements Generating Random Numbers practice.
1 public class Newton { public static double sqrt(double c) { double epsilon = 1E-15; if (c < 0) return Double.NaN; double t = c; while (Math.abs(t - c/t)
13 X 11 Java Lecture 3 CS 1311 Structure 13 X 11.
Lecture 15: I/O and Parsing
Java File I/O. File I/O is important! Being able to write and read from files is necessary and is also one common practice of a programmer. Examples include.
MOD III. Input / Output Streams Byte streams Programs use byte streams to perform input and output of 8-bit bytes. This Stream handles the 8-bit.
Today’s lecture Review of Chapter 1 Go over homework exercises for chapter 1.
1 MATH METHODS THAT RETURN VALUES. 2 JAVA'S MATH CLASS.
Computer Programming w/ Eng. Applications
Core Java Lecture 4-5. What We Will Cover Today What Are Methods Scope and Life Time of Variables Command Line Arguments Use of static keyword in Java.
Objects contains data and methods Class – type of object Create class first Then object or instance String – defined class String s; // creates instance.
L2:CSC © Dr. Basheer M. Nasef Lecture #2 By Dr. Basheer M. Nasef.
Self Check 1.Which are the most commonly used number types in Java? 2.Suppose x is a double. When does the cast (long) x yield a different result from.
Procedural programming in Java
METHOD OVERRIDING Sub class can override the methods defined by the super class. Overridden Methods in the sub classes should have same name, same signature.
Java Programming, 3e Concepts and Techniques Chapter 3 Section 63 – Manipulating Data Using Methods – Day 2.
Lecture 27 Exam outline Boxing of primitive types in Java 1.5 Generic types in Java 1.5.
Mathematical Operators  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to Computers and Programming in.
Java Programming, 3e Concepts and Techniques Chapter 3 Manipulating Data Using Methods.
©2004 Brooks/Cole Chapter 1: Getting Started Sections Covered: 1.1Introduction to Programming 1.2Constructing a Java Program 1.3The print() and println()
Copywrite 2003 Walter Savitch These slides are for the exclusive use of students in CSE 11 at UCSD, Winter quarter They may not be copied or used.
School of Computing Science CMT1000 Ed Currie © Middlesex University Lecture 4: 1 CMT1000: Introduction to Programming Ed Currie Lecture 5a: Input and.
Introduction to Python
COMP 14: I/O and Boolean Expressions May 24, 2000 Nick Vallidis.
1 The First Step Learning objectives write Java programs that display text on the screen. distinguish between the eight built-in scalar types of Java;
Introduction to Methods. How do we use Methods in Java? Let us start by creating one which displays “hello” on Dos Prompt.
2.2 Information on Program Appearance and Printing.
Example 1 :- Handling integer values public class Program1 { public static void main(String [] args) { int value1, value2, sum; value1 = Integer.parseInt(args[0]);
Java Programming, 2E Introductory Concepts and Techniques Chapter 3 Manipulating Data Using Methods.
SOFTWARE TECHNOLOGY - I CONSTANTS VARIABLES DATA TYPES.
C# B 1 CSC 298 Writing a C# application. C# B 2 A first C# application // Display Hello, world on the screen public class HelloWorld { public static void.
© The McGraw-Hill Companies, 2006 Chapter 4 Implementing methods.
CSI 1390: Introduction to Computers TA: Tapu Kumar Ghose Office: STE 5014
Console Input. So far… All the inputs for our programs have been hard-coded in the main method or inputted using the dialog boxes of BlueJ It’s time to.
Introduction to Programming David Goldschmidt, Ph.D. Computer Science The College of Saint Rose Java Fundamentals (Comments, Variables, etc.)
Basic Java Programming CSCI 392 Week Two. Stuff that is the same as C++ for loops and while loops for (int i=0; i
Copyright © Curt Hill Java Looking at our first console application in Eclipse.
CS 112 Department of Computer Science George Mason University CS 112 Department of Computer Science George Mason University Final Review Lecture 14.
5 BASIC CONCEPTS OF ANY PROGRAMMING LANGUAGE Let’s get started …
Chapter 2: Java Fundamentals
Overloading There is another aspect to polymorphism: Overloading Overloading is not overriding. In Turkish: Overridding: eskisini (geçersiz kılmak) Overloading:
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 2 - Introduction to Java Applications Outline 2.1Introduction 2.2A Simple Program: Printing a.
Introduction to C++ Version 1.1. Topics C++ Structure Primitive Data Types I/O Casting Strings Control Flow.
CSC Programming I Lecture 6 September 4, 2002.
Getting Started.
5-Dec-15 Sequential Files and Streams. 2 File Handling. File Concept.
Fall 2002CS 150: Intro. to Computing1 Streams and File I/O (That is, Input/Output) OR How you read data from files and write data to files.
CSC 1010 Programming for All Lecture 3 Useful Python Elements for Designing Programs Some material based on material from Marty Stepp, Instructor, University.
By Mr. Muhammad Pervez Akhtar
Computer Programming A simple example /* HelloWorld: A simple C program */ #include int main (void) { printf (“Hello world!\n”); return.
Copyright © Curt Hill Simple I/O Input and Output using the System and Scanner Objects.
Simple Console Output. What we will briefly discuss System.out.println( … ) System.out.print( … ) System.out.printf( … )
CSC 110 – Intro to Computing - Programming
Chapter 2: Data and Expressions. Variable Declaration In Java when you declare a variable, you must also declare the type of information it will hold.
Objectives You should be able to describe: Interactive Keyboard Input
Introduction to Computer Science / Procedural – 67130
2.5 Another Java Application: Adding Integers
Lecture Note Set 1 Thursday 12-May-05
I/O Basics.
Intro to Java.
I/O Streams- Basics Byte Streams and Character Streams
Lesson 2: Building Blocks of Programming
Java Looking at our first console application in Eclipse
Exception Handling Contents
F II 2. Simple Java Programs Objectives
Presentation transcript:

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