CompSci 001 7.1 Today’s topics Java Numbers Iteration Upcoming More Java Reading Great Ideas, Chapter 3.

Slides:



Advertisements
Similar presentations
CompSci Today’s topics Java Recursion in Graphics Writing a Class Simple Animation Upcoming Simulation Reading Great Ideas, Chapters 5.
Advertisements

1 Chapter 2 Introduction to Java Applications Introduction Java application programming Display ____________________ Obtain information from the.
Copyright © 2012 Pearson Education, Inc. Chapter 6 More Conditionals and Loops Java Software Solutions Foundations of Program Design Seventh Edition John.
CS 178: Programming with Multimedia Objects Aditya P. Mathur Professor of Computer Sciences Purdue University, West Lafayette Sept 9, 2004 Last update:
Computer Science 1620 Variables and Memory. Review Examples: write a program that calculates and displays the average of the numbers 45, 69, and 106.
C-1 University of Washington Computer Programming I Lecture 3: Variables, Values, and Types © 2000 UW CSE.
COMPSCI 125 Spring 2005 ©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 3: Numeric Data *Variables *Numeric data.
Loops – While, Do, For Repetition Statements Introduction to Arrays
Aalborg Media Lab 23-Jun-15 Software Design Lecture 6 “Conditionals and Loops”
Chapter 4 Loops Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Chapter 3 Numerical Data. Topics Variables Numeric data types Assignment Expressions.
Loops Repetition Statements. Repetition statements allow us to execute a statement multiple times Often they are referred to as loops Like conditional.
Lecture Review (If-else Statement) if-else statement has the following syntax: if ( condition ) { statement1; } else { statement2; } The condition.
Lecture 7. Review Homework 1 (sample solution) Project 1 will be assigned next week –Draw a picture (whatever you want) in the world by using turtles.
CS0007: Introduction to Computer Programming Introduction to Arrays.
Spring 2008 Mark Fontenot CSE 1341 Principles of Computer Science I Note Set 4.
CompSci Today’s topics Java The Database Program Upcoming Recursion Reading Great Ideas, Chapter 4.
Chapter 5 Loops Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved.
2/4/2003CSCI 150: Introduction to Computer Science1 Introduction to Computer Science CSCI 150 Section 002 Session 6 Dr. Richard J. Bonneau IONA Technologies.
Java Programming, 3e Concepts and Techniques Chapter 3 Section 65 – Manipulating Data Using Methods – Java Applet.
CPS Today’s topics Java Information Retrieval Upcoming Database Programs Reading Great Ideas, Chapter 4.
Chapter 5 Control Structures: Loops 5.1 The while Loop The while loop is probably the most frequently used loop construct. The while loop is a conditional.
SNU IDB Lab. 1 Great Ideas of CS with Java Part 1 WWW & Computer programming in the language Java Ch 1: The World Wide Web Ch 2: Watch out: Here comes.
CompSci Today’s topics Java Review Just a bunch of headings meant to trigger questions A contraction of previous notes Upcoming Midterm Exam Reading.
Copyright © 2014, 2010, 2006 Pearson Education, Inc. 1 Chapter 1 Introduction to Functions and Graphs.
Copyright © 2012 Pearson Education, Inc. Chapter 6 More Conditionals and Loops Java Software Solutions Foundations of Program Design Seventh Edition John.
1 Three C++ Looping Statements Chapter 7 CSIS 10A.
Static Methods. 2 Objectives Look at how to build static (class) methods Study use of methods calling, parameters, returning values Contrast reference.
1 BUILDING JAVA PROGRAMS CHAPTER 2 PRIMITIVE DATA AND DEFINITE LOOPS.
CPS Today’s topics Java Applications Graphics Upcoming Review for Midterm Exam Reading Great Ideas, Chapters 5.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and.
Loops (cont.). Loop Statements  while statement  do statement  for statement while ( condition ) statement; do { statement list; } while ( condition.
Java Applet Basics (2). The Body Mass Index Calculator.
CompSci Today’s topics Java Applications Simulation Upcoming Software Engineering (Chapter 7) Reading Great Ideas, Chapters 6.
Repetition. Loops Allows the same set of instructions to be used over and over again Starts with the keyword loop and ends with end loop. This will create.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
In the last lesson we discussed about: Casting Precedence The “=“ used as an assignment operator Made a calculate average program.
Chapter 6: Repetition Continued. 2 Validity Checks What’s weak about the following code ? do { s1 = JOptionPane.showInputDialog (“Enter a number: ”);
SNU OOPSLA Lab. 1 Great Ideas of CS with Java Part 1 WWW & Computer programming in the language Java Ch 1: The World Wide Web Ch 2: Watch out: Here comes.
CompSci Today’s topics Java Writing Functions/Methods Upcoming Information Retrieval Reading Great Ideas, Chapter 4.
PHY281 Scientific Java Programming ArraysSlide 1 Arrays In this section we will learn how about storing groups of related variables that share the same.
1 For Loops l From Chapter 9 l A shorthand way of coding count loops.
Copyright © 2012 Pearson Education, Inc. Chapter 5: Loops.
Topic: Control Statements. Recap of Sequence Control Structure Write a program that accepts the basic salary and allowance amount for an employee and.
Today’s topics Java Input More Syntax Upcoming Decision Trees More formal treatment of grammers Reading Great Ideas, Chapter 2.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
Today’s topics Java Looping Upcoming Arrays in Java Reading Great Ideas, Chapter 3.
CS Class 04 Topics  Selection statement – IF  Expressions  More practice writing simple C++ programs Announcements  Read pages for next.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and.
CompSci Today’s topics Java Recursion Upcoming Graphics Reading Great Ideas, Chapter 4 (begin Chapter 5 for graphics)
Lecture 3: More Java Basics Michael Hsu CSULA. Recall From Lecture Two  Write a basic program in Java  The process of writing, compiling, and running.
Today’s topics Java Upcoming Reading Recursion in Graphics
HelloWorld.java import java.awt.*; public class HelloWorld extends
Today’s topics Java Information Retrieval Upcoming Database Programs
Chapter 4 Repetition Statements (loops)
Manipulating Pictures, Arrays, and Loops part 2
Today’s topics Java Arrays Upcoming Functions Reading
Building Java Programs
Today’s topics Java Applications Upcoming Reading Graphics
Chapter 4 LOOPS © Bobby Hoggard, Department of Computer Science, East Carolina University / These slides may not be used or duplicated without permission.
Powers & Roots When we say an integer or real number is “raised to a power”, we are talking about exponents.
Outline Altering flow of control Boolean expressions
Programming Funamental slides
Chapter 2 Programming Basics.
Building Java Programs
Looping and Repetition
Control Structures.
Presentation transcript:

CompSci Today’s topics Java Numbers Iteration Upcoming More Java Reading Great Ideas, Chapter 3

CompSci Numbers  Have talked about and used numbers before  Have built-in or “primitive types”  int for whole numbers  double for numbers that may include fractions  One of simplest uses for integers is counting  The following example counts number of times button is pressed  Includes statement: numTotal = numTotal + 1; o Not an equality! o Evaluate right ; copy into left

CompSci Counting Example public class Count extends java.applet.Applet implements ActionListener { TextField mQuery, mTotal; Button bCount; int noTotal = 0; public void init (){ mQuery = new TextField(80); mQuery.setText( "Keep track of attendance by pushing the button."); bCount = new Button("Register"); mTotal = new TextField(40); bCount.addActionListener(this); add(mQuery); add(bCount); add(mTotal); mTotal.setText("The total attendance is “ + noTotal); }

CompSci Counting Example (P2) public void actionPerformed(ActionEvent event) { Object cause = event.getSource(); if (cause == bCount) // (could have omitted) { noTotal = noTotal + 1; mTotal.setText("The total attendance is " + noTotal); }  Can extend this to multiple counts

CompSci Tallies public class Tallies extends java.applet.Applet implements ActionListener { TextField mQuery,mAnsStu, mAnsFac, mAnsSta, mTotal; Button bStudents,bFaculty,bStaff; int noStu=0, noFac=0, noSta=0, noTotal=0; public void init (){ mQuery = new TextField(80); mQuery.setText( "Keep track of attendance by pushing the buttons."); bStudents = new Button("Students"); bFaculty = new Button("Faculty"); bStaff = new Button("Staff"); mAnsStu = new TextField(12); mAnsFac = new TextField(12);

CompSci Tallies (p2) mAnsSta = new TextField(12); mTotal = new TextField(80); bStudents.addActionListener(this); bFaculty.addActionListener(this); bStaff.addActionListener(this); add(mQuery); add(bStudents); add(bFaculty); add(bStaff); add(mTotal); add(mAnsStu); add(mAnsFac); add(mAnsSta); mTotal.setText("The total attendance is " + noTotal + " Subtotals are shown below."); mAnsStu.setText(noStu + " students"); mAnsFac.setText(noFac + " faculty"); mAnsSta.setText(noSta + " staff"); }

CompSci Tallies (p3) public void actionPerformed(ActionEvent event) { Object cause = event.getSource(); if (cause == bStudents){ noStu = noStu + 1; noTotal = noTotal + 1; mTotal.setText("The total attendance is " + noTotal + " Subtotals are shown below."); mAnsStu.setText(noStu + " students"); } // similar blocks follow for faculty & staff

CompSci Tallies (p4) if (cause == bFaculty){ noFac = noFac + 1; noTotal = noTotal + 1; mTotal.setText("The total attendance is " + noTotal + " Subtotals are shown below."); mAnsFac.setText(noFac + " faculty"); } if (cause == bStaff){ noSta = noSta + 1; noTotal = noTotal + 1; mTotal.setText("The total attendance is " + noTotal + " Subtotals are shown below."); mAnsSta.setText(noSta + " staff"); }

CompSci Numbers  Have classes similar to TextField to do I/O with integers  Have used IntField methods before  Here is class declaration Public class IntField { public IntField( ); public IntField(int size); public void setInt(int number); public void setInt(); public int getInt(); }

CompSci Doubles  Similarly for doubles, we have public class DoubleField { public DoubleField( ); public DoubleField(int size); public void setDouble(double num); public double getDouble( ); }  Have already discussed expressions using int s and double s

CompSci Calculations  Can use doubles and DoubleFields for real computations  Here is an applet to calculate volume of a cylinder  Need to supply o radius o length  Use formula o volume = (area-of-end) * length = Pi * radius * radius * length

CompSci Calculate volume cylinder public class Numbers extends java.applet.Applet implements ActionListener { TextField instruct, result, mRadius, mLength; DoubleField gRadius, gLength; Button bCompute; double radius, length, cylVol, PI= ; public void init (){ instruct = new TextField(72); instruct.setText("Please enter radius and length below."); mRadius = new TextField(9); mRadius.setText("radius:"); mLength = new TextField(9); mLength.setText("length:");

CompSci Calculate volume cylinder (p2) gRadius = new DoubleField(10); gLength = new DoubleField(10); result = new TextField(72); result.setText( "The volume of the cylinder is: " + cylVol); bCompute = new Button("Compute"); bCompute.addActionListener(this); add(instruct); add(mRadius); add(gRadius); add(mLength); add(gLength); add(bCompute); add(result); }

CompSci Calculate volume cylinder (p3) public void actionPerformed(ActionEvent event){ Object cause = event.getSource(); if (cause == bCompute){ length = gLength.getDouble(); radius = gRadius.getDouble(); cylVol = PI * radius * radius * length; result.setText( "The volume of the cylinder is: " + cylVol); }

CompSci Iteration  Iteration -- also called repetition or looping  Iteration by Button pushing  Often need to repeat a calculation with minor changes  Sometimes refine previous solution  Sometimes calculate successive values in a series  Can do this under control of a Button  Applets have the concept of iteration built into their very nature  Waiting for a Button to be pressed implies a loop

CompSci Iteration by Button pushing example public class ButCompound extends java.applet.Applet implements ActionListener { TextField mInstruct, mBalance; DoubleField gRate, gPrinc, gPay; Button bStart, bNextInstallment; double rate, princ, pay, balance; int months; public void init(){ mInstruct = new TextField(80); mInstruct.setText( "Enter principal, rate, payment, then press Start");

CompSci Iteration by Button pushing example.2 gPrinc = new DoubleField(10); gRate = new DoubleField(10); gPay = new DoubleField(10); mBalance = new TextField(80); bStart = new Button("Start"); bNextInstallment = new Button("Next Installment"); bStart.addActionListener(this); bNextInstallment.addActionListener(this); add(mInstruct); add(gPrinc); add(gRate); add(gPay); add(bStart); add(bNextInstallment); add(mBalance); }

CompSci Iteration by Button pushing example.3 public void actionPerformed(ActionEvent event) { Object cause = event.getSource(); if (cause == bStart){ princ = gPrinc.getDouble(); rate = gRate.getDouble()/12; pay = gPay.getDouble(); months = 0; balance = princ; mInstruct.setText( "Press Next Installment for next Balance"); mBalance.setText( "Start with a balance of " + balance); }

CompSci Iteration by Button pushing example.4 if (cause == bNextInstallment) { months = months + 1; balance = balance*(1.0 + rate) - pay; mBalance.setText("After " + months + " months at " + 100*rate*12 + "% and payments of " + pay + " the balance is " + balance); }