CS 121 – Intro to Programming:Java - Lecture 8 Announcements Current Owl assignment is due Wednesday. Next Owl assignment - arrays I - will be up by tomorrow.

Slides:



Advertisements
Similar presentations
Chapter 8: Arrays.
Advertisements

CS324e - Elements of Graphics and Visualization A Little Java A Little Python.
1 More on Arrays and Loops Reading for this Lecture: –Section 5.4, , Break and Continue in Loops Arrays and For-each Loops Arrays and Loops.
CS 121 – Intro to Programming:Java - Lecture 9 Announcements Two new Owl assignments up - they’re a bit more challenging. Programming assignment 5 is due.
CS 106 Introduction to Computer Science I 02 / 18 / 2008 Instructor: Michael Eckmann.
1 Arrays b An array is an ordered list of values An array of size N is indexed from zero to N-1 scores.
CS 106 Introduction to Computer Science I 10 / 04 / 2006 Instructor: Michael Eckmann.
Program Elements We can now examine the core elements of programming (as implemented in Java) We focuse on: data types variable declaration and use, constants.
COMP 110 Introduction to Programming Mr. Joshua Stough October 24, 2007.
Java Types float, double long, int, short byte char boolean Objects Objects are created by : Declaring a reference and calling a constructor. Basic types.
Arrays in Java Selim Aksoy Bilkent University Department of Computer Engineering
Copyright 2008 by Pearson Education Building Java Programs Chapter 7 Lecture 7-2: Tallying and Traversing Arrays reading: 7.1 self-checks: #1-9 videos:
CS 121 – Intro to Programming:Java - Lecture 3 Announcements Course home page: Owl due Friday;
Week 4-5 Java Programming. Loops What is a loop? Loop is code that repeats itself a certain number of times There are two types of loops: For loop Used.
CS 121 – Intro to Programming:Java - Lecture 10 Announcements Two Owl assignments up, due 17th, 22nd Another up today, due 11/30 Next programming assignment.
Copyright 2006 by Pearson Education 1 Building Java Programs Chapters 3-4: Using Objects.
Arrays (Part 1) Computer Science Erwin High School Fall 2014.
STRINGS CMSC 201 – Lab 3. Overview Objectives for today's lab:  Obtain experience using strings in Python, including looping over characters in strings.
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
The string data type String. String (in general) A string is a sequence of characters enclosed between the double quotes "..." Example: Each character.
Basic Java Programming CSCI 392 Week Two. Stuff that is the same as C++ for loops and while loops for (int i=0; i
Palindromes revisited Here's a simpler program for checking palindromes: int nums[100]; int i = 0, a; cin >> a; while(a > 0) { nums[i++] = a; cin >> a;
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.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
CS 121 – Intro to Programming:Java - Lecture 7 Announcements A new Owl assignment is available. Programming assignment 4 is due on Thursday - hand in on.
8-1 Chapter 8: Arrays Arrays are objects that help us organize large amounts of information Today we will focuses on: –array declaration and use –bounds.
Computer Programming 12 Mr. Jean April 24, The plan: Video clip of the day Upcoming Quiz Sample arrays Using arrays More about arrays.
CS 121 – Intro to Programming:Java - Lecture 5 Announcements Course home page: Owl due Thursday at 11; another one up today. Third programming assignment.
Chapter 4: Control Structures II
Introduction to Programming
CSC Programming I Lecture 6 September 4, 2002.
Chapter 7 Arrays: Part 1 of 2. © 2004 Pearson Addison-Wesley. All rights reserved7-2 Arrays Arrays are objects that help us organize large amounts of.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
***** SWTJC STEM ***** Chapter 7 cg 68 What Are Arrays? An array is a simple but powerful way to organize and store large amounts of data and information.
The character data type char. Character type char is used to represent alpha-numerical information (characters) inside the computer uses 2 bytes of memory.
Chapter 6. else-if & switch Copyright © 2012 Pearson Education, Inc.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
Copyright 2006 by Pearson Education 1 Building Java Programs Chapter 7: Arrays.
A Simple Java Program //This program prints Welcome to Java! public class Welcome { public static void main(String[] args) { public static void main(String[]
Coding Bat: Ends in ly Given a string of even length, return a string made of the middle two chars, so the string "string" yields "ri". The string.
Copyright 2010 by Pearson Education Building Java Programs Chapter 7 Lecture 7-3: Arrays for Tallying; Text Processing reading: 7.1, 4.4 self-checks: #1-9.
Introduction to Computing Concepts Note Set 15. JOptionPane.showMessageDialog Message Dialog Allows you to give a brief message to the user Can be used.
Jeopardy Print Me Which loop? Call Me Name Me My Mistake Q $100 Q $200 Q $300 Q $400 Q $500 Q $100 Q $200 Q $300 Q $400 Q $500 Final Jeopardy.
© 2007 Pearson Addison-Wesley. All rights reserved2-1 Character Strings A string of characters can be represented as a string literal by putting double.
CS 121 – Intro to Programming:Java - Lecture 12 Announcements New Owl assignment up soon (today?) For this week: read Ch 8, sections 0 -3 Programming assignment.
Array Size Arrays use static allocation of space. That is, when the array is created, we must specify the size of the array, e.g., int[] grades = new int[100];
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
When constructing a two-dimensional array, specify how many rows and columns are needed: final int ROWS = 3; final int COLUMNS = 3; String[][] board =
Lecture 10. Review (Char) Character is one of primitive data types of variable (such as integer and double) –Character variable contains one character.
1 Arrays Chapter 8. Objectives You will be able to Use arrays in your Java programs to hold a large number of data items of the same type. Initialize.
1 Lecture # 2. * Introducing Programming with an Example * Identifiers, Variables, and Constants * Primitive Data Types * Byte, short, int, long, float,
Introduction to programming in java Lecture 22 Arrays – Part 2 and Assignment No. 3.
CS 121 – Intro to Programming:Java - Lecture 4 Announcements Course home page: Owl due soon; another.
Chapter 7 Arrays…. 7-2 Arrays An array is an ordered list of values An array of size N is indexed from.
Methods. Creating your own methods Java allows you to create custom methods inside its main body. public class Test { // insert your own methods right.
CSE 110: Programming Language I Matin Saad Abdullah UB 1222.
CS 121 – Intro to Programming:Java - Lecture 11 Announcements Inheritance Owl assignment due 11/30 at 11 am Programming assignment six due Friday 12/3.
Multiple variables can be created in one declaration
Repetition.
Arrays We often want to organize objects or primitive data in a way that makes them easy to access and change. An array is simple but powerful way to.
class PrintOnetoTen { public static void main(String args[]) {
Scope of variables class scopeofvars {
Arrays in Java.
Building Java Programs
Repetition Statements
Chapter 2: Java Fundamentals cont’d
Random Numbers while loop
More on iterations using
Presentation transcript:

CS 121 – Intro to Programming:Java - Lecture 8 Announcements Current Owl assignment is due Wednesday. Next Owl assignment - arrays I - will be up by tomorrow morning Programming assignment 5 is up, due next week. Hand in protocol will be announced on-line tomorrow afternoon. Late rules in effect - late programs are reduced in value by half. Preregistration coming - what should you take?

Get going on program five! Here’s one thing you can do…the bouncing ball example.. Form your groups.. me your ideas, etc..

We’ve seen Java at two levels: the statement level - mechanisms for getting specific, often low-level jobs, done, e.g. assignment stmts, println, etc. the (class and) object level - mechanisms for modeling things (objects) according to an” Objects” (repositories of state) served by methods (machinery for realizing behaviors) scheme Now we’re back to a new and very important idea in statement-level thinking: arrays. Basically, arrays give us a new way to think about variables.

Think about: students in a class; seats on an airplane, rooms in a motel, positions in the deli line at a super-market. In all cases: Many variables required for representation There’s an indexing scheme for locating / identifying the variables in question Student 7 Seat 23B Room 201 Deli-line position 77 some indexing schemes are more natural than others some are two-dimensional

public class ArrayTest1{ public static void main(String[] args) { int[] firstArray = new int[10]; for(int j = 0; j < 10; j++) firstArray[j] = j*j; System.out.println("here they come"); for(int j = 0; j < 10; j++) System.out.println(firstArray[j]); } }

public class BasicArray { final static int LIMIT = 15; final static int MULTIPLE = 10; public static void main (String[] args) { int[] list = new int[LIMIT]; for (int index = 0; index < LIMIT; index++) list[index] = index * MULTIPLE; list[5] = 999; // change one array value for (int index = 0; index < LIMIT; index++) System.out.print (list[index] + " "); }

public class Test{ // all characters have a position in Unicode // Ascii comes first : // when an integer op is applied to a character, it // is automatically converted into an integer public static void main(String[] args){ int i,j; i = (int) 'a'; j = (int) 'A'; System.out.println(i + " " + j); System.out.println('a' - 'A'); System.out.println('e' - 'a');} } ´œ ----jGRASP exec: java Test œœßœ97 65 œœßœ32 œœßœ4

public class LetterCount{ // // Reads a sentence from the user and counts the number of // uppercase and lowercase letters contained in it. // public static void main (String[] args) { ConsoleWindow c = new ConsoleWindow(50,40, "my window"); final int NUMCHARS = 26; int[] upper = new int[NUMCHARS]; int[] lower = new int[NUMCHARS]; char current; // the current character being processed int other = 0; // counter for non-alphabetics c.out.println ("Enter a sentence:"); String line = c.input.readLine(); // reads line c.out.println("line length " + line.length());

// Count the number of each letter occurence for (int ch = 0; ch = 'A' && current = 'a' && current <= 'z') lower[current-'a']++; else other++; }

// Print the results c.out.println (); for (int letter=0; letter < upper.length; letter++) { c.out.print ( (char) (letter + 'A') ); c.out.print (": " + upper[letter]); c.out.print ("\t\t" + (char) (letter + 'a') ); c.out.println (": " + lower[letter]); } c.out.println (); c.out.println ("Non-alphabetic characters: " + other); } }

We’re going to write an application that rolls a pair of dice times and reports the results profile of the rolls (e.g. how many 2,3, etc came up. public class DiceTester{ public static void main(String[] args){ Dice d = new Dice(); d.multiToss(10000); d.showScoreboard(); } }

import java.util.Random; public class Dice{ Random r; int[] scoreboard = new int[13]; public Dice(){ r = new Random(); initializeScoreboard(); } public void initializeScoreboard(){ for(int j = 0; j < 13; j++) scoreboard[j] = 0;}

public int tossDie(){ return (1+r.nextInt(6)); } public int throwDice(){ return(tossDie() + tossDie()); } public void multiToss(int tossCount){ int score; for (int j = 0; j < tossCount; j++){ score = throwDice(); scoreboard[score]++; } }

public void showScoreboard(){ for(int j = 2; j < 13; j++) System.out.println("toss of " + j + " " + scoreboard[j]); }

Results: œœœ œœ´œ ----jGRASP exec: java DiceTester œœßœtoss of œœßœtoss of 3543 œœßœtoss of œœßœtoss of œœßœtoss of œœßœtoss of œœßœtoss of œœßœtoss of œœßœtoss of œœßœtoss of œœßœtoss of œœßœ œœ©œ ----jGRASP

import element.*; public class PolyTester{ public static void main(String[] args){ ConsoleWindow c = new ConsoleWindow(); DrawingWindow d = new DrawingWindow(500,200); Pt[] poly = new Pt[5]; int x, y; c.out.println("enter 5 pts - 10 ints - separated by CRs"); for(int j = 0; j < 5; j++) { x = c.input.readInt(); y = c. input.readInt(); poly[j] = new Pt(x,y); } for(int j = 1; j < 5; j++) { L = new Line(poly[j-1], poly[j]); L.drawOn(d); } L = new Line(poly[4],poly[0]); // final connector L.drawOn(d); }