CS 121 – Intro to Programming:Java - Lecture 3 Announcements Course home page: Owl due Friday;

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

Lecture 1: Comments, Variables, Assignment. Definitions The formal (human-readable) instructions that we give to the computer is called source code The.
Chapter 7 User-Defined Methods. Chapter Objectives  Understand how methods are used in Java programming  Learn about standard (predefined) methods and.
CS 121 – Intro to Programming:Java - Lecture 2 Announcements Course home page: First Owl assigment.
5/17/ Programming Constructs... There are several types of programming constructs in JAVA. - If-else construct or ternary operator - while - do-while.
Mock test review Revision of Activity Diagrams for Loops,
Random (1) Random class contains a method to generate random numbers of integer and double type Note: before using Random class, you should add following.
Copyright 2008 by Pearson Education 1 Building Java Programs Chapter 2 Lecture 2-1: Expressions and Variables reading:
If statements Chapter 3. Selection Want to be able to do a statement sometimes, but not others if it is raining, wear a raincoat. Start first with how.
COMP 14: Primitive Data and Objects May 24, 2000 Nick Vallidis.
COMP Classes Yi Hong May 22, Announcement  Lab 2 & 3 due today.
COMP More About Classes Yi Hong May 22, 2015.
CS 121 – Intro to Programming:Java - Lecture 6 Announcements Fourth programming assignment now up, due in Friday. OWL assignments due as indicated MidTerm:
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.
Conditional If Week 3. Lecture outcomes Boolean operators – == (equal ) – OR (||) – AND (&&) If statements User input vs command line arguments.
COMP 110 Spring Announcements Computers in class on Friday: Lab Office Hours: Monday 12-2 New students see me after class Administrative Changes.
Lecture 2: Static Methods, if statements, homework uploader.
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
CSC 204 Programming I Loop I The while statement.
CS 11 java track: lecture 1 Administrivia need a CS cluster account cgi-bin/sysadmin/account_request.cgi need to know UNIX
© 2005 Lawrenceville Press Slide 1 Chapter 5 Relational Operators Relational OperatorMeaning =greater than.
CSCI S-1 Section 6. Coming Soon Homework Part A – Friday, July 10, 17:00 EST Homework Part B – Tuesday, July 14, 17:00 EST Mid-Term Quiz Review – Friday,
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.
October 28, 2015ICS102: For Loop1 The for-loop and Nested loops.
Can we talk?. In Hello World we already saw how to do Standard Output. You simply use the command line System.out.println(“text”); There are different.
Copyright 2008 by Pearson Education 1 Building Java Programs Chapter 2 Lecture 2-1: Expressions and Variables reading:
Procedural programming in Java Methods, parameters and return values.
CS 121 – Intro to Programming:Java - Lecture 5 Announcements Course home page: Owl due Thursday at 11; another one up today. Third programming assignment.
BEGINNING PROGRAMMING.  Literally – giving instructions to a computer so that it does what you want  Practically – using a programming language (such.
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.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
1 Basic Java Constructs and Data Types – Nuts and Bolts Looking into Specific Differences and Enhancements in Java compared to C.
COMP 110 Classes Luv Kohli October 1, 2008 MWF 2-2:50 pm Sitterson 014.
Copyright 2010 by Pearson Education 1 Building Java Programs Chapter 2 Lecture 2-1: Expressions and Variables reading:
User Defined Methods Methods are used to divide complicated programs into manageable pieces. There are predefined methods (methods that are already provided.
1 Building Java Programs Chapter 2 Lecture 2-1: Expressions and Variables reading:
CSCI S-1 Section 4. Deadlines for Homework 2 Problems 1-8 in Parts C and D – Friday, July 3, 17:00 EST Parts E and F – Tuesday, July 7, 17:00 EST.
A Introduction to Computing II Lecture 1: Java Review Fall Session 2000.
Midterm Review Tami Meredith. Primitive Data Types byte, short, int, long Values without a decimal point,..., -1, 0, 1, 2,... float, double Values with.
Computer Science A 1. Course plan Introduction to programming Basic concepts of typical programming languages. Tools: compiler, editor, integrated editor,
1 Lecture # 2. * Introducing Programming with an Example * Identifiers, Variables, and Constants * Primitive Data Types * Byte, short, int, long, float,
CS 121 – Intro to Programming:Java - Lecture 4 Announcements Course home page: Owl due soon; another.
Introduction to Programming G50PRO University of Nottingham Unit 6 : Control Flow Statements 2 Paul Tennent
Chapter 7 Control Structures. Java has very flexible three looping mechanisms. You can use one of the following three loops:  while Loop  do...while.
Methods. Creating your own methods Java allows you to create custom methods inside its main body. public class Test { // insert your own methods right.
Programming in Java Transitioning from Alice. Becomes not myFirstMethod but …. public static void main (String[] arg) { // code for testing classes goes.
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.
Midterm preview.
CompSci 230 S Programming Techniques
Michele Weigle - COMP 14 - Spr 04 Catie Welsh February 21, 2011
Chapter 7 User-Defined Methods.
Java Course Review.
An Introduction to Java – Part I
LRobot Game.
An Introduction to Java – Part I, language basics
Building Java Programs
Java so far Week 7.
CS 200 Primitives and Expressions
Take out a piece of paper and PEN.
class PrintOnetoTen { public static void main(String args[]) {
CS 200 Primitives and Expressions
Anatomy of a Java Program
Chapter 2 Programming Basics.
Building Java Programs
Building Java Programs
Building Java Programs
CS Week 2 Jim Williams, PhD.
Loops CGS3416 Spring 2019 Lecture 7.
Building Java Programs
Building Java Programs
Presentation transcript:

CS 121 – Intro to Programming:Java - Lecture 3 Announcements Course home page: Owl due Friday; another one coming. Second programming assignment now up, due in class, on paper, next Tuesday Read ch 3 through section 3.9 for thursday MidTerm: Monday, 10/18, , Thompson 102,106 (I’ll say more later..) Office hours - now posted; held in LGRT 220 I will accept late programming assignments on Thursday

Java’s Object Model Classes - a class is a blueprint or template for an object an Object is an example or an instantiation of a class definition an instance variable or attribute is a characteristic of an Object a method is an activity that serves an object Example: A Student class is a template for a student Attributes - name, age, credits, year, etc Methods - getAge, creditsLeft, calcGPA, and so forth Writing a class definition prepares this template Different students mean different objects -- with different attribute values!

Conditional Statements in Java Conditional statements (and looping statements) are flow of control constructions At a primitive level, Java programs are made up of statements, and it often makes sense to 1)have statements repeat in a systematic way; and 2)have statements execute conditionally

if (n % 2 == 0) System.out.println(“n is even”); Lots going on here: statement says: “if the remainder after dividing n by 2 is equal to (==) 0, then report that n is an even number if (n % 2 != 0) System.out.println(“n is odd”); else System.out.println(“n is even”); An important point: (n % 2 == 0) is a boolean expression (returns a boolean value) -- a boolean must go into the test slot of an if stmt!

The for loop for(int j = 0; j j = j + 1 System.out.println(“I will not talk in class”); } for(int j = 3; j < 12; j = j + 2){ System.out.println(j);} // prints what? for(int j = 30; j > 20; j = j - 2){ System.out.println(j);} // prints what?

for(int j = 3; j < 12; j = j*j){ System.out.println(j);} // prints what? for(int j = 3; j != 12; j = j + 2){ System.out.println(j);} // prints what?

The while loop An especially flexible construction int k = 0; while (k < 100){ System.out.println(k); k = k + 1; } // (or: k++;) Note also-> 2 stmt body int k = 0; while (k < 100){ System.out.println(k); k = 2*k; } int k = 1; while (k < 100){ System.out.println(k); k = 2*k; }

public class Rows{ final char Blank = ' '; // a constant! public void makeRow(int k, char sym){ // prints k symbols (sym) in a row for(int j = 0; j < k; j++) System.out.print(sym); } public void newLine(){System.out.println();}// goes to new line public void spacedRow(int k, char sym){ // prints k symbols in a row, every other on is blank for(int j = 0; j < k; j++) if (j % 2 == 0) System.out.print(sym); else System.out.print(Blank); } }

public class RowTester{ public static void main(String[] args){ Rows r = new Rows(); for(int j = 0; j < 5; j++){ r.makeRow(10,'*'); r.newLine(); } } } This program makes 5 rows of 10 stars: *********** etc

public class RowTester{ public static void main(String[] args){ Rows r = new Rows(); for(int j = 0; j < 5; j++){ r.makeRow(j+2,'*'); r.newLine(); } } }

The result: ** *** **** ***** ******

Random numbers Java has a Random class (in the java.util package) It produces pseudo-random numbers Random r = new Random(); vs Random r = new Random(303); r.nextInt(); vs r.nextInt(1000 r.nextDouble() -> produces a random value between 0 and 1.0

import java.util.Random; public class RandomTester{ public static void main(String[] args) { Random r1 = new Random(202); Random r2 = new Random(303); System.out.println(" from r1: " + r1.nextInt()); System.out.println(" from r2: " + r2.nextInt()); } } œ from r1: œ from r2:

import java.util.Random; public class RowTester{ public static void main(String[] args){ Random randy = new Random(); Rows r = new Rows(); int oddSize; for(int j = 0; j < 5; j++){ oddSize = randy.nextInt(40); r.makeRow(oddSize,'*'); r.newLine(); }

Result of one run: ************************** **** ********* ****** *********************************** Result of a second run: ************************************** ************ ******************* *****

A real use for random numbers (more or less..) We know that √2 = ~ This says that if I choose 1000 numbers between 1 and 2, about 414 should fall below √2 -- the others should be above this value. We can actually use this (probabilistic) technique to compute this values (unimportant) and other values (important!) Here’s the code…

import java.util.Random; public class RootTwo{ public static void main(String[] args){ Random r = new Random(); int ct = 0; double val; for(int j = 0; j < 1000; j++){ val = r.nextDouble(); if ((1 + val) * (1 + val) < 2) ct++; } System.out.println("Square root of 2 is " + (1 + (double)ct/1000)); } } jGRASP exec: java RootTwo œœßœSquare root of 2 is 1.408