1 CSC 201: Computer Programming I Lecture 2 B. S. Afolabi.

Slides:



Advertisements
Similar presentations
1 Arrays An array is a special kind of object that is used to store a collection of data. The data stored in an array must all be of the same type, whether.
Advertisements

STRING AN EXAMPLE OF REFERENCE DATA TYPE. 2 Primitive Data Types  The eight Java primitive data types are:  byte  short  int  long  float  double.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Fall 2013.
Big Java by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved. It is common to use two nested loops when filling or searching: for.
Copyright 2010 by Pearson Education Building Java Programs Chapter 7 Lecture 7-1: Arrays reading: 7.1 self-checks: #1-9 videos: Ch. 7 #4.
Copyright 2008 by Pearson Education Building Java Programs Chapter 7 Lecture 7-1: Arrays reading: 7.1 self-checks: #1-9 videos: Ch. 7 #4.
Loops – While Loop Repetition Statements While Reading for this Lecture, L&L, 5.5.
Java Intro. Strings “This is a string.” –Enclosed in double quotes “This is “ + “another “ + “string” –+ concatenates strings “This is “ “ string”
Lecture 15 Arrays: Part 1 COMP1681 / SE15 Introduction to Programming.
CS102--Object Oriented Programming Lecture 6: – The Arrays class – Multi-dimensional arrays Copyright © 2008 Xiaoyan Li.
Strings as objects Strings are objects. Each String is an instance of the class String They can be constructed thus: String s = new String("Hi mom!");
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.
Introduction to Information and Computer Science Computer Programming Lecture c This material (Comp4_Unit5c), was developed by Oregon Health and Science.
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
Lecture 10 Recursion. public class recursionDemo { public static void main(String[] args) { System.out.println("TriCount for n = 5 is... " + triCount(5));
Java means Coffee Java Coffee Beans The name “JAVA” was taken from a cup of coffee.
5-Aug-2002cse Arrays © 2002 University of Washington1 Arrays CSE 142, Summer 2002 Computer Programming 1
The basics of the array data structure. Storing information Computer programs (and humans) cannot operate without information. Example: The array data.
Chapter 8: Collections: Arrays. 2 Objectives One-Dimensional Arrays Array Initialization The Arrays Class: Searching and Sorting Arrays as Arguments The.
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.
Chapter 2 Elementary Programming
A First Look at Java Chapter 2 1/29 & 2/2 Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education - Prentice Hall, 2010.
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.
DT249-Information Systems Research Practice Programming Revision Lecture 2 Lecturer: Patrick Browne.
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.
Component 4: Introduction to Information and Computer Science Unit 5: Overview of Programming Languages, Including Basic Programming Concepts Lecture 3.
Java Variables, Types, and Math Getting Started Complete and Turn in Address/Poem.
CSC 1051 – Algorithms and Data Structures I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
Reading input from the console input. Java's console input The console is the terminal window that is running the Java program I.e., that's the terminal.
Introduction to array: why use arrays ?. Motivational example Problem: Write a program that reads in and stores away 5 double numbers After reading in.
Spring 2009 Programming Fundamentals I Java Programming XuanTung Hoang Lecture No. 8.
Java – Variables and Constants By: Dan Lunney. Declaring Variables All variables must be declared before they can be used A declaration takes the form:
An Introduction to Java – Part 1 Erin Hamalainen CS 265 Sec 001 October 20, 2010.
A Introduction to Computing II Lecture 1: Java Review Fall Session 2000.
Java Scanner Class Keyboard Class. User Interaction So far when we created a program there was no human interaction Our programs just simply showed one.
©2016 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved. CSC 110 – INTRO TO COMPUTING - PROGRAMMING Switch Statement.
© 2007 Lawrenceville Press Slide 1 Chapter 4 Review Assignment Statement An assignment statement gives a value to a variable. Assignment can take several.
Programming Fundamentals 2: Libraries/ F II Objectives – –utilize some useful Java libraries e.g. String, Scanner, HashMap, and Random.
Chapter 5: Arrays in Java. The objectives of this chapter are:  1. To discuss the creation and use of Arrays.   2. To continue to use the String class.
A.P. Computer Science Input is NOT tested on the AP exam, but if we want to do any labs, we need input!!
CS 115 OBJECT ORIENTED PROGRAMMING I LECTURE 11 GEORGE KOUTSOGIANNAKIS 1 Copyright: 2015 Illinois Institute of Technology_ George Koutsogiannakis.
1 Lecture # 2. * Introducing Programming with an Example * Identifiers, Variables, and Constants * Primitive Data Types * Byte, short, int, long, float,
Object Oriented Programming Lecture 2: BallWorld.
Introduction to programming in java
IAS 1313: OBJECT ORIENTED PROGRAMMING Week 3: Data Type, Control Structure and Array Prepared by: Mrs Sivabalan1.
Building Java Programs Chapter 7 Arrays Copyright (c) Pearson All rights reserved.
Elementary Programming
Yanal Alahmad Java Workshop Yanal Alahmad
Lecture 2: Data Types, Variables, Operators, and Expressions
Computer Programming Methodology Input and While Loop
Data types, Expressions and assignment, Input from User
Building Java Programs
Building Java Programs
An Introduction to Java – Part I
INPUT STATEMENTS GC 201.
مساق: خوارزميات ومبادئ البرمجة الفصل الدراسي الثاني 2016/2015
An Introduction to Java – Part I, language basics
Building Java Programs
Java so far Week 7.
Building Java Programs
Building Java Programs
python.reset() Also moving to a more reasonable room (CSE 403)
Lecture Notes – Week 2 Lecture-2
Arrays in Java.
Building Java Programs
Building Java Programs
More on iterations using
Presentation transcript:

1 CSC 201: Computer Programming I Lecture 2 B. S. Afolabi

 Student:  VOISSAPP  Download the android app from the Google play store.  2

public class Stream { public static void main(String[] args) { String s1 = "abc"; String s2 = s1 + ""; // New object, but contains same text as s1 String s3 = s1; // Same object as s1 String s4 = s1.toString(); // Same object as s1 System.out.println("s1 and s2 identical objects: " + (s1 == s2)); System.out.println("s1 and s3 identical objects: " + (s1 == s3)); System.out.println("s1 and s4 identical objects: " + (s1 == s4)); System.out.println("s1 and s2 contain same text: " + (s1.equals(s2))); System.out.println("s1 and s3 contain same text: " + (s1.equals(s3))); } 3

Count the number of e’s in a string static int ecount(String s) { int ecount = 0; for (int i=0; i<s.length(); i++) if (s.charAt(i) == ’e’) ecount++; return ecount; } 4

Arrays  An array is a collection of variables, called elements.  It has a given length l and a given element type t.  The elements are indexed by the integers 0, 1, …, l-1. 5

Array creation and access  array creation expression: new t[l]  where l is an expression of type int all elements of the new array are initialized to 0 (when t is byte, char, short, int, or long) or 0.0 (when t is float or double) or false (when t is boolean) 6

Creating and using one-dimensional arrays // Roll a die, count frequencies int[] freq = new int[6]; // all initialized to 0 for (int i=0; i<1000; i++) { int die = (int)(1 + 6 * Math.random()); Freq[die-1] += 1; } for (int c=1; c<=6; c++) System.out.println(c + " came up " + freq[c-1] + " times"); 7

Creating an array of Strings // Create an array of the strings "A0", "A1",..., "A19" String[] number = new String[20]; // all initialized to null for (int i=0; i<number.length; i++) number[i] = "A" + i; for (int i=0; i<number.length; i++) System.out.println(number[i]); 8

Using an initialized array static int[] days = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; static boolean checkdate(int mth, int day) { return (mth >= 1) && (mth = 1) && (day <= days[mth-1]); } 9

Multi-dimensional arrays // Create a lower triangular array of the form // 0.0 // // final int SIZE = 3; double[][] a = new double[SIZE][]; for (int i=0; i<SIZE; i++) a[i] = new double[i+1]; // Use a nested array initializer to create an array similar to the above double[][] b = { { 0.0 }, { 0.0, 0.0 }, { 0.0, 0.0, 0.0 } }; 10

Input from Keyboard  To input from the keyboard, Java provides a special class called the Scanner class  Scanner belong to the package called util  To access the package, the import command is used  this is done by placing import java.util.*  The * means that all classes in the package are made available to the compiler 11

Calculating cost by reading from keyboard import java.util.* ; // in order to get access to the scanner class. public class Find_Cost { public static void main(String[] args) { Scanner Keyboard = new Scanner(system.in); //create a scanner object Double price, tax; System.out.println(“**** Product Price Check ****”); System.out.print(“Enter initial price: ”); //prompt for input price = keyboard.nextDouble(); //input method called System.out.print(“Enter tax rate: ”); //prompt for input tax= keyboard.nextDouble(); //input method called price = price* (1 + tax/100); //perform the calculation System.out.println(“Cost after tax = ” + price); } 12

 13