CSC 243 - Java Programming, Fall, 2008 Week 2: Java Data Types, Control Constructs, and their C++ counterparts, September 9.

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

CERTIFICATION OBJECTIVES Use Class Members Develop Wrapper Code & Autoboxing Code Determine the Effects of Passing Variables into Methods Recognize when.
Exception Handling The purpose of exception handling is to permit the program to catch and handle errors rather than letting the error occur and suffer.
Text File I/O. Text Files and Binary Files Files that are designed to be read by human beings, and that can be read or written with an editor are called.
For use of IST410 Students only Arrays-1 Arrays. For use of IST410 Students only Arrays-2 Objectives l Declaring arrays l Instantiating arrays l Using.
Chapter 10.
Shlomo Hershkop1 Introduction to java Class 1 Fall 2003 Shlomo Hershkop.
1 Chapter 4 Language Fundamentals. 2 Identifiers Program parts such as packages, classes, and class members have names, which are formally known as identifiers.
1 Java intro Part 3. 2 Arrays in Java Store fixed number of values of a given type Arrays are objects –have attributes –must be constructed Array declaration:
CS102--Object Oriented Programming Lecture 6: – The Arrays class – Multi-dimensional arrays Copyright © 2008 Xiaoyan Li.
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.
CMSC 341 Introduction to Java Based on tutorial by Rebecca Hasti at
Sadegh Aliakbary Sharif University of Technology Fall 2011.
Example 1 :- Handling integer values public class Program1 { public static void main(String [] args) { int value1, value2, sum; value1 = Integer.parseInt(args[0]);
Classes, Objects, Arrays, Collections and Autoboxing Dr. Andrew Wallace PhD BEng(hons) EurIng
LESSON 2 CREATING A JAVA APPLICATION JAVA PROGRAMMING Compiled By: Edwin O. Okech [Tutor, Amoud University]
Recitation 1 CS0445 Data Structures Mehmud Abliz.
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
CSC204 – Programming I Lecture 4 August 28, 2002.
CSC Java Programming, Fall, 2008 Week 2: Java Data Types, Control Constructs, and their C++ counterparts, September 4.
Basic Java Programming CSCI 392 Week Two. Stuff that is the same as C++ for loops and while loops for (int i=0; i
Geoff Holmes and Bernhard Pfahringer COMP206-08S General Programming 2.
Chapter 9 1 Chapter 9 – Part 1 l Overview of Streams and File I/O l Text File I/O l Binary File I/O l File Objects and File Names Streams and File I/O.
Arrays Chapter 7. 2 "All students to receive arrays!" reports Dr. Austin. Declaring arrays scores : Inspecting.
Exceptions. Exception Abnormal event occurring during program execution Examples –Manipulate nonexistent files FileReader in = new FileReader("mumbers.txt“);
JAVA Array 8-1 Outline  Extra material  Array of Objects  enhanced-for Loop  Class Array  Passing Arrays as Arguments to Methods  Returning Arrays.
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.
Arrays Chapter 8. What if we need to store test scores for all students in our class. We could store each test score as a unique variable: int score1.
2D-Arrays Quratulain. Learning Objectives Two-dimensional arrays Declaration Initialization Applications.
Exception. Runtime Error Consider the following program: public class BadArray { public static void main(String[] args) { // Create an array with three.
Using Data Within a Program Chapter 2.  Classes  Methods  Statements  Modifiers  Identifiers.
An Introduction to Java – Part 1 Dylan Boltz. What is Java?  An object-oriented programming language  Developed and released by Sun in 1995  Designed.
Documentation Array and Searching. Documentation rules Easy rules: –Naming convention for variables, constants and methods Difficult rules: –Professional.
Sadegh Aliakbary Sharif University of Technology Fall 2010.
OOP in Java : © W. Milner 2005 : Slide 1 Java and OOP Part 2 – Classes and objects.
1 CSC 201: Computer Programming I Lecture 2 B. S. Afolabi.
BEGINNING PROGRAMMING.  Literally – giving instructions to a computer so that it does what you want  Practically – using a programming language (such.
Everything is an object (CH-2) Manipulating Objects with References. Manipulating Objects with References. String s; String s = “IS2550” String s = new.
Core Java Introduction Byju Veedu Ness Technologies httpdownload.oracle.com/javase/tutorial/getStarted/intro/definition.html.
Exceptions Chapter 16 This chapter explains: What as exception is Why they are useful Java exception facilities.
CMSC 202 Arrays 2 nd Lecture. Aug 6, Array Parameters Both array indexed variables and entire arrays can be used as arguments to methods –An indexed.
Exceptions in C++. Exceptions  Exceptions provide a way to handle the errors generated by our programs by transferring control to functions called handlers.
 2008 Pearson Education, Inc. All rights reserved. 1 Arrays and Vectors.
Exceptions. Exception  Abnormal event occurring during program execution  Examples Manipulate nonexistent files FileReader in = new FileReader("mumbers.txt“);
Chapter 15 Strings as Character Arrays
CSC Java Programming, Fall, 2008 Week 3: Objects, Classes, Strings, Text I/O, September 11.
Java Part I By Wen Fei, HAO. Program Structure public class ClassName { public static void main(String[] args) { program statements } user defined methods.
An Introduction to Java – Part 1 Erin Hamalainen CS 265 Sec 001 October 20, 2010.
Arrays Chapter 7. 2 Declaring and Creating Arrays Recall that an array is a collection of elements all of the _____________ Array objects in Java must.
CSC Java Programming, Spring, 2010 Week 2: Java Data Types, Control Constructs, and their C++ counterparts.
Grouping Data Together Often we want to group together a number of values or objects to be treated in the same way e.g. names of students in a tutorial.
Programming Fundamentals 2: Libraries/ F II Objectives – –utilize some useful Java libraries e.g. String, Scanner, HashMap, and Random.
CS 31 Discussion, Week 7 Faisal Alquaddoomi, Office Hours: BH 2432, W 4:30-6:30pm, F 12:30-1:30pm.
CS 116 OBJECT ORIENTED PROGRAMMING II LECTURE 4 GEORGE KOUTSOGIANNAKIS Copyright: 2016 Illinois Institute of Technology/George Koutsogiannakis 1.
 2005 Pearson Education, Inc. All rights reserved Arrays.
Alice and Java Unit 7 1. Day 1  Objective: Gain an introduction to Java and Eclipse  Essential skill: DM-1: Use technology to advance critical thinking.
Chapter 9 Introduction to Arrays Fundamentals of Java.
CSC Java Programming, Fall, 2008 August 28, 2008, class 2 Tuesday, September 2 uses Monday’s schedule!
Object Oriented Programming Lecture 2: BallWorld.
Java Programming Language Lecture27- An Introduction.
CSC Java Programming, Spring, 2010
Objects, Classes, Program Constructs
Testing and Exceptions
Introduction to Programming in Java
Building Java Programs
Building Java Programs
CSC Java Programming, Spring, 2012
CSC Java Programming, Spring, 2009
Review for Midterm 3.
Presentation transcript:

CSC Java Programming, Fall, 2008 Week 2: Java Data Types, Control Constructs, and their C++ counterparts, September 9

References GNU make docs. has on-line docs. class lib. /export/home/faculty/parson/JavaLang on bill.kutztown.edu Make sure that /usr/jdk/jdk1.6.0_02/bin is near the front of UNIX PATH. Follow instructions in the JavaShellSetup.pdf document (accessible via my home page at to set up and verify your Java compilation environment.JavaShellSetup.pdfhttp://faculty.kutztown.edu/parson

Boolean example code /export/home/faculty/parson/JavaLang/week1/bool Make sure you have a copy of this. cp –pr ~parson/JavaLang/week1/bool ~/JavaLang/week1/bool Program verifies valid command line args. 1. Program reads command line into an array of floats, raising an Exception if any of the command line arguments is not a float. 2. Verify that array is in ascending order using &&. 3. Verify that array is in ascending order using ||.

Booleans Boolean variables and constants boolean isSortedAnd = true, isOutOfOrderOr = false ; Boolean expressions isOutOfOrderOr || ! isLessThanOrEqual(numbers[i], numbers[i+1]) Condition control constructs use booleans if (isSortedAnd != ! isOutOfOrderOr) { Conditional expressions use booleans System.exit(isSortedAnd ? 0 : 1);

Class java.lang.System System.in is equivalent to C++ cin. import java.util.Scanner ; Scanner inputScanner = new Scanner(System.in); System.out and System.err are equivalent to C++ cout and cerr. System.out.println("sort result = " + isSortedAnd); System.exit(0) causes the process to exit with status of 0, meaning there is no error. A non-0 exits status signifies an error.

Creating Java Arrays Arrays are constructed using new (like C++). “float [] numbers ;” creates a null array reference. float [] numbers = new float [ INTEGER_SIZE ] ; Java implicitly initializes an array of numbers with 0. Booleans gets false, chars get ‘\0000’. float [] numbers = {1.9F, 2.9F, 3.4F, 3.5F} ; Myclass [] myobject = new Myclass[ INTEGER_SIZE ] ; Java initializes an array of object references with null. Java objects and arrays created using “new” need not be “deleted.” JAVA HAS GARBAGE COLLECTION!

Using Java Arrays “String [] args” is the parameter for Java main. public static void main(String args[]) { main is needed for running a class from the UNIX command line. Expression “numbers.length” gives the number of elements in the array. if (args.length == 0) { float [] numbers = new float [ args.length ] A subscript access and element of an array numbers[i] = Float.parseFloat(args[i]);

Helper class java.util.Arrays Utility methods for manipulating arrays. public static int[] copyOf(int[] original, int newLength); public static void fill(int[] a, int val); public static void sort(int[] a); public static int binarySearch(int[] a, int key); public static T[] copyOf(T[] original, int newLength); public static void sort(T[] a, Comparator c) ;Comparator Comparator int compare(T o1, T o2) returns -1 or 0 or 1 for o1 o2 respectively.T

Multidimensional Arrays int [][] matrix ; // an array of arrays, null int [][] matrix = new int[4][3]; // 4 rows of 3 int [][] matrix = { // See Figure 6.11, p. 204 {1, 2, 3}, {4, 5, 6}, {7, 8, 9}, {10, 11, 12} }; matrix[0][0] is the initial element of the initial row matrix[3][2] is the last element of the last sub-array (last column of the last row)

“Ragged Arrays” int [][] triangle = { // p. 205 {1, 2, 3, 4, 5}, {2, 3, 4, 5}, {3, 4, 5}, {4, 5}, {5} }; int [][] triangle = new int [5][]; for (int i = 0 ; i < triangle.length ; i++) { triangle[i] = new int [triangle.length – i]; } triangle[1].length gives the length of the second sub-array triangle[1], which == 4.

Handling an exception from a library method Read on-line doc on java.lang.Float.parseFloat try { numbers[i] = Float.parseFloat(args[i]); } catch (NumberFormatException nfexp) { System.err.println("format error on " + args[i] + ": " + nfexp.getMessage()); isSortedAnd = false ; isOutOfOrderOr = true ; // Initialize array element to a default value. numbers[i] = 0.0F ; }

Notes on the makefile If you run a test that you intend to fail – System.exit(NON-0) – then put a “-” in front of that command invocation in the makefile. -java $(PACKAGE).$(BASENAME) fred Also, since your error messages are sent to System.err, you need to redirect System.err to a file to capture its output to use with diff. >> $(BASENAME).out 2>&1 >> $(BASENAME).out 2>$(BASENAME).err

Programming practices Always handle exceptions! We may handle some by explicitly ignoring them. Always use { curly braces } for control blocks. Use coding standards that we discuss in class. Write Javadoc documentation. Use both healthy and degenerate tests. » Ignoring these rules will cost you points.