Basic Java Programming CSCI 392 Week Two. Stuff that is the same as C++ for loops and while loops for (int i=0; i<10; i++) if else if (count != 10) increment,

Slides:



Advertisements
Similar presentations
AP Computer Science Anthony Keen. Computer 101 What happens when you turn a computer on? –BIOS tries to start a system loader –A system loader tries to.
Advertisements

IT 325 OPERATING SYSTEM C programming language. Why use C instead of Java Intermediate-level language:  Low-level features like bit operations  High-level.
Arrays in JAVA CS 340 Software Design Muhammad Talha.
Written by: Dr. JJ Shepherd
8-May-15 Additional control structures. 2 The if-else statement The if-else statement chooses which of two statements to execute The if-else statement.
1 G54PRG Programming Lecture 1 Amadeo Ascó Adam Moore 6 Decision Making, Control Structures & Error Handling.
Shlomo Hershkop1 Introduction to java Class 1 Fall 2003 Shlomo Hershkop.
16-Jun-15 Additional control structures. 2 The if-else statement The if-else statement chooses which of two statements to execute The if-else statement.
1 LECTURE#7: Console Input Overview l Introduction to Wrapper classes. l Introduction to Exceptions (Java run-time errors). l Console input using the BufferedReader.
BASIC JAVA. Hello World n // Hello world program public class MyFirstJavaProgram { public static void main(String args[]) { char c = 'H'; String s =
Programming in Java; Instructor:Alok Mehta Objects, Classes, Program Constructs1 Programming in Java Objects, Classes, Program Constructs.
CS 225 Java Review. Java Applications A java application consists of one or more classes –Each class is in a separate file –Use the main class to start.
Fundamental Programming Structures in Java: Comments, Data Types, Variables, Assignments, Operators.
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;
MSc IT Programming Methodology (2). MODULE TEAM Dr Aaron Kans Dr Sin Wee Lee.
JAVA PROGRAMMING PART II.
CSC3170 Introduction to Database Systems
Introduction to Programming Prof. Rommel Anthony Palomino Department of Computer Science and Information Technology Spring 2011.
Example 1 :- Handling integer values public class Program1 { public static void main(String [] args) { int value1, value2, sum; value1 = Integer.parseInt(args[0]);
1 Course Lectures Available on line:
Java. Why Java? It’s the current “hot” language It’s almost entirely object-oriented It has a vast library of predefined objects It’s platform independent.
Review Review Review. Concepts Comments: definition, example, different types of comments Class: definition, example Object: definition, example Data.
SOFTWARE TECHNOLOGY - I CONSTANTS VARIABLES DATA TYPES.
General Features of Java Programming Language Variables and Data Types Operators Expressions Control Flow Statements.
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
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 Syntax CSE301 University of Sunderland Harry R Erwin, PhD.
JAVA Tokens. Introduction A token is an individual element in a program. More than one token can appear in a single line separated by white spaces.
Java means Coffee Java Coffee Beans The name “JAVA” was taken from a cup of coffee.
Basics of Java IMPORTANT: Read Chap 1-6 of How to think like a… Lecture 3.
Outline Character Strings Variables and Assignment Primitive Data Types Expressions Data Conversion Interactive Programs Graphics Applets Drawing Shapes.
From C++ to Java A whirlwind tour of Java for C++ programmers.
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
Netprog: Java Intro1 Crash Course in Java. Netprog: Java Intro2 Why Java? Network Programming in Java is very different than in C/C++ –much more language.
Chapter 2: Java Fundamentals
CIS 260: App Dev I. 2 Programs and Programming n Program  A sequence of steps designed to accomplish a task n Program design  A detailed _____ for implementing.
1 Programming Java Java Basics. 2 Java Program Java Application Program Application Program written in general programming language Applet Program running.
Introduction to Java Lecture Notes 3. Variables l A variable is a name for a location in memory used to hold a value. In Java data declaration is identical.
College Board A.P. Computer Science A Topics Program Design - Read and understand a problem's description, purpose, and goals. Procedural Constructs -
Apr, 2011 Dating with Java Larry Li. Objective Hello world program Setup development environment Data types and variables Operators and Expressions Control.
BEGINNING PROGRAMMING.  Literally – giving instructions to a computer so that it does what you want  Practically – using a programming language (such.
1-Dec-15 Additional control structures. 2 The if-else statement The if-else statement chooses which of two statements to execute The if-else statement.
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.
1 Basic Java Constructs and Data Types – Nuts and Bolts Looking into Specific Differences and Enhancements in Java compared to C.
A Simple Java Program //This program prints Welcome to Java! public class Welcome { public static void main(String[] args) { public static void main(String[]
Exceptions Chapter 16 This chapter explains: What as exception is Why they are useful Java exception facilities.
Reference Learning Java DDC publishing Herst, Yamauchi, Adler COP 3331 Object Oriented Analysis and Design Java Part I.
A Introduction to Computing II Lecture 1: Java Review Fall Session 2000.
Java Basics. Tokens: 1.Keywords int test12 = 10, i; int TEst12 = 20; Int keyword is used to declare integer variables All Key words are lower case java.
Another example of input import java.io.*; //import java.lang.Integer.*; class ExampleofInput2 { private static BufferedReader stdin = new BufferedReader(
1 Lecture # 2. * Introducing Programming with an Example * Identifiers, Variables, and Constants * Primitive Data Types * Byte, short, int, long, float,
Exceptions and Error Handling. Exceptions Errors that occur during program execution We should try to ‘gracefully’ deal with the error Not like this.
Object Oriented Programming Lecture 2: BallWorld.
 It is a pure oops language and a high level language.  It was developed at sun microsystems by James Gosling.
Information and Computer Sciences University of Hawaii, Manoa
Lecture 2 D&D Chapter 2 & Intro to Eclipse IDE Date.
Lecture Note Set 1 Thursday 12-May-05
Haidong Xue Summer 2011, at GSU
Chapter 2.
Object-Oriented Programming
Introduction to Programming in Java
Operators Laboratory /11/16.
Yong Choi School of Business CSU, Bakersfield
Introduction to Java Programming
An Introduction to Java – Part I, language basics
An overview of Java, Data types and variables
Java Programming Review 1
Agenda Types and identifiers Practice Assignment Keywords in Java
Presentation transcript:

Basic Java Programming CSCI 392 Week Two

Stuff that is the same as C++ for loops and while loops for (int i=0; i<10; i++) if else if (count != 10) increment, decrement, etc result *= factorial--; switch statements switch (answer) { case 'y': case 'Y': some stuff break; default: }

Java Statements Not in C++ Labeling blocks of code outerloop: for (int i=0;…) for (int j=0; … if (something_bad) break outerloop; Exception Handling try { value = x/y; } catch (ArithmeticException exceptvar) { System.out.println("Math Error"); }

Basic Data Types Boolean boolean - 1 bit Integers byte- 8 bits short- 16 bits int- 32 bits long- 64 bits Characters char- 16 bits Floats float double Strings String still be careful when comparing

Boolean - So what? This C++ feature is not legal in Java: int factorial, result = 1;... while (factorial) result *= factorial--; But neither is this common C++ error: if (count = 10) // not legal Java

Variable Declaration Oddities You must initialize a variable before it can be used, so most declarations include an initialization. You can set a "constant's" value at run time. final int count = getCount(); Arrays require both a definition and declaration. int[] myarray; myarray = new int[25]; int[] myarray = new int[25]; char[] name = {'B', 'o', 'b'};

Type Checking You can assign small sized variables into bigger variables, but big variables don't fit into small variables. int X = 99; long Y = 0; Y = X; // legal X = Y; // not legal X = (int) Y; // legal

Comments Just like C /* multi-line */ // single line Except… /** document comment **/ not thrown away by compiler

Simple Output Printing to "Standard Output" System.out.print ("Hello " + name ); System.out.println (); Formatted Output NumberFormat layout = NumberFormat.getNumberInstance(); layout.setMaximumFractionDigits(4); double x = 12.3, y = 45.6; String outstr = layout.format(x/y); System.out.println("x/y = " + outstr);

Input - not so simple import java.io.* ; public class array_test_1 { public static void main(String[] args) throws Exception { InputStreamReader stdio = new InputStreamReader(System.in); BufferedReader instream = new BufferedReader (stdio); int count; System.out.print ("Enter an integer: "); String indata = instream.readLine(); count = Integer.parseInt (indata); for (int i=0; i<count; i++) System.out.print (i + " "); System.out.println(); } improvement: catch the NumberFormatException