NotesWeek4.Sept 13-17

NotesWeek4.Sept 13-17 - CS 180 Problem Solving and Object...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: CS 180 Problem Solving and Object Oriented Programming Fall 2010 hNp://www.cs.purdue.edu/homes/apm/courses/CS180Fall2010/ Notes for Week 4: September 1317, 2010 Aditya Mathur This week: 1. 2. 3. 4. 5. 6. Feedback for Week 3 Review PrimiUve types: conversion Scanner: reading data Type mismatch WriUng simple programs to solve simple problems Department of Computer Science Purdue University West LafayeNe, IN, USA Readings and Exercises for Week 3 Readings: Chapter 2: 2.5, 2.6, 2.7, 2.8, 2.9 Exercises: 2.7, 2.8, 2.10, 2.15, 2.21 9/12/2010 CS 180. Fall 2010. Week 4 2 Special help sessions Sundays: 24pm LWSN B158 9/12/2010 CS 180. Fall 2010. Week 4 3 Feedback for Week 3 9/12/2010 CS 180. Fall 2010. Week 4 4 Q1. The lab exercises were useful (Use scale of 110: 10 most useful, 1 not useful at all) (a) 810 (b) 47 (c) 13 (d) Missed Week 3 lab 9/12/2010 CS 180. Fall 2010. Week 4 5 Q2. The recitaUon exercises were useful (Use scale of 110: 10 most useful, 1 not useful at all) (a) 810 (b) 47 (c) 13 (d) Missed 3 recitaUon 9/12/2010 CS 180. Fall 2010. Week 4 6 Q3. The recitaUon instructor was helpful. (Use scale of 110: 10 most helpful, 1 not helpful at all) (a) 810 (b) 47 (c) 13 (d) Missed week 3 recitaUon 9/12/2010 CS 180. Fall 2010. Week 4 7 Q4. I understand how to use the Scanner class. (a) 810 (b) 47 (c) 13 (d) Missed week 3 lecture 9/12/2010 CS 180. Fall 2010. Week 4 8 Q5. I understand primiUve types. (a) 810 (b) 47 (c) 13 (d) Missed week 3 lecture 9/12/2010 CS 180. Fall 2010. Week 4 9 Q6. I understand what is a variable. (a) 810 (b) 47 (c) 13 (d) Missed week 3 lecture 9/12/2010 CS 180. Fall 2010. Week 4 10 Q7. So far I am liking the course (10 liking a lot, 1 not liking at all). (a) 810 (b) 47 (c) 13 (d) Missed week 2 lecture 9/12/2010 CS 180. Fall 2010. Week 4 11 Any project related quesUons? 9/12/2010 CS 180. Fall 2010. Week 4 12 What is Algorithmic Thinking? It is the ability to map a problem to an algorithm that consists of a precisely specified sequence of computaUonal steps In most cases the algorithm so derived is then mapped into a computer program wriNen in a suitable language. The enUre process of mapping a problem to a correctly funcUoning program is also known as computer programming. This step as as intellectually challenging as the step that maps a problem to an algorithm. Algorithmic thinking is CRITICAL for success as a computer scienUst. 9/12/2010 CS 180. Fall 2010. Week 4 13 Review 9/12/2010 CS 180. Fall 2010. Week 4 14 PrimiUve types: short, int, long Real world en+ty or expression Type Possible name in Java PopulaUon of a country int countryPopulaUon Age of a paUent (in years) short paUentAge Number of different ways to arrange 15 books in a bookshelf Difference between two integers long bookArrangementCount int or long long diff Number of web sites numberOfWebSites 9/12/2010 CS 180. Fall 2010. Week 4 15 PrimiUve types: float, double Real world en+ty or expression Type Possible name in a Java program height Height of a person float VoUng percentage float votePercent Wavelength of green light double wavelengthLIght Price of a Ucket float UcketPrice double pi (Note: PI is a constant in Java) CS 180. Fall 2010. Week 4 16 9/12/2010 PrimiUve types: boolean Real world en+ty or expression Type Possible name in a Java program Value of x<y; boolean result she/he drives a car boolean canDriveCar Class ended boolean classEnded 9/12/2010 CS 180. Fall 2010. Week 4 17 PrimiUve types: char Real world en+ty or expression Type Possible name in a Java program middleIniUal Middle iniUal char LeNer of the alphabet char leNer US currency sign char usCurrency 9/12/2010 CS 180. Fall 2010. Week 4 18 Names, Variables, Constants Length unlimited, case sensiUve. Dog and dog are different names. ConvenUon: All class names begin with an uppercase leNer; all other names begin with a lower case leNer. Constants: 12, 3.99, true, "Hello", `Z' Variable: Has a name, a type, and its value may change during program execuUon. 9/12/2010 CS 180. Fall 2010. Week 4 19 DeclaraUons Every newly used name must be declared before it is used. This could be the name of a variable, a constant, an object or a class. int age; float height, area; String name; String firstName="Harry"; boolean alive; int x=1, y=0; 9/12/2010 CS 180. Fall 2010. Week 4 20 Assignments An assignment statement allows assigning the value of an expression to a variable. float p=x*y+z; // p gets the value of x*y+z boolean q=x<y; // q gets the value of x<y double totalPrice=priceItem1+priceItem2; String firstName="Mary", lastName= "Jones"; String name= firstName+" "+lastName; 9/12/2010 CS 180. Fall 2010. Week 4 21 Scanner: methods Type match/mismatch 9/12/2010 CS 180. Fall 2010. Week 4 22 Scanner: methods: Examples int age; age=source.nextInt(); float price; price=source.nextFloat(); String name; name=source.nextLine(); String name; name=source.next(); boolean alive; alive=source.nextBoolean(); 9/12/2010 CS 180. Fall 2010. Week 4 23 Formasng a string: Example public class FormatTest{ public staUc void main(String args ){ double price=4.977798; String formaNedPrice=String.format("%8.2f", price); System.out.println(formaNedPrice); } } Output: 4.98 9/12/2010 CS 180. Fall 2010. Week 4 24 Let us write a simple Java program: The problem Write a Java program to compute the net sale in dollars given the price of each Ucket in dollars and the number of Uckets sold. 9/12/2010 CS 180. Fall 2010. Week 4 25 Problem: Understanding This is an easy problem! 9/12/2010 CS 180. Fall 2010. Week 4 26 Problem: Design of soluUon Keep this step independent of Java! Step 1: Get data Step 2: Compute total sale Step 3: Display total sale 9/12/2010 CS 180. Fall 2010. Week 4 27 Problem: Refine soluUon: Get data Keep this step independent of Java! Step 1.1: Prompt for price Step 1.2: Read price Step 1.3: Prompt for number of Uckets sold Step 1.4: Read number of Uckets sold 9/12/2010 CS 180. Fall 2010. Week 4 28 Problem: Refine soluUon: Compute total sale Keep this step independent of Java! Step 2: Total sale=price of a Ucket * number of Uckets sold 9/12/2010 CS 180. Fall 2010. Week 4 29 Code the soluUon in Java. Test it. 9/12/2010 CS 180. Fall 2010. Week 4 30 Week 4: September 1317, 2010 Hope you enjoyed this week! QuesUons? Contact your recitaUon instructor. Make full use of our office hours. 9/12/2010 CS 180. Fall 2010. Week 4 31 ...
View Full Document

{[ snackBarMessage ]}

Ask a homework question - tutors are online