Programming Fundamentals I CS 110 Central Washington University

  • Slides: 15
Download presentation
Programming Fundamentals I CS 110, Central Washington University Spring 2017 CS 110: Programming Fundamentals

Programming Fundamentals I CS 110, Central Washington University Spring 2017 CS 110: Programming Fundamentals I, Winter 2017 1 9/9/2021

Announcements q q HW #6 is due may 25 night, 11: 59 pm? Final

Announcements q q HW #6 is due may 25 night, 11: 59 pm? Final project proposal/description and pseudo-code (1 page maximum) were due, keep working on it… CS 110: Programming Fundamentals I, Winter 2017 2 9/9/2021

Poll Question … review Which of the following statements are false about UML diagrams?

Poll Question … review Which of the following statements are false about UML diagrams? A. B. C. D. E. F. UML includes a set of graphic notation techniques for creating visual models of object-oriented programs. UML permits people to discuss methods, classes, routines, etc. on a high-level, without needing to discuss the low-level details. A UML diagram of a class consists of the class's name, its field declarations, and the method declarations, and high-level details about these From a UML diagram, you can infer the code that exists in the body of each method. Method return types are placed after the method declaration, separated by a colon Access specifiers in a UML diagram are denoted as + for public, and - for private Jet // A Class that defines an object of type Jet public class Jet { - num. Seats : int - color : String // fields private String color; private int num. Seats; - get. Num. Seats() : int + set. Color(c : String) : void // methods public void set. Color(String c){ color = c; } private int get. Num. Seats(){ return num. Seats; } CS 110: Programming Fundamentals I, Winter 2017 }3 9/9/2021

Quick review of last lecture. . . 1 // A Class that defines an

Quick review of last lecture. . . 1 // A Class that defines an object of type Bicycle 2 public class Bicycle { 3 4 // fields 5 private String color; 6 private int num. Gears; 7 private int num. Spokes; 8 9 // methods 10 public void set. Color(String bicycle. Color){ 11 color = bicycle. Color; 12 } 13 14 public void set. Num. Gears(int number. Of. Gears){ 15 num. Gears = number. Of. Gears; 16 } 17 18 public void set. Num. Gears(){ 19 num. Spokes = 32; 20 } 21 22 public Bicycle(int number. Of. Spokes){ 23 num. Spokes = number. Of. Spokes; 24 } 25 } CS 110: Programming Fundamentals I, Winter 2017 A class … q is a template that is used to create many instances (objects) of a certain type q is code that describes a particular type of object q specifies the data that an object can hold (the object's fields), and the actions that an object can perform (the object's methods). A constructor is. . . q a “method” that is invoked when an object of a certain class is created q a method with the same name as the Class name A constructor has. . . q has no return type Methods … q Can be overloading, meaning that there are two o more methods with the same name but different argument (parameter) lists. 4 9/9/2021

Today. . . A full class and program (main) example with methods CS 110:

Today. . . A full class and program (main) example with methods CS 110: Programming Fundamentals I, Winter 2017 5 9/9/2021

A full class and program (main) example with methods Write a program that q

A full class and program (main) example with methods Write a program that q q Asks the user to input the width and height of three rectangles, and creates three objects of type Rectangle, defined according to the UML diagram below Outputs the areas of the three rectangles, relying on the get. Area method in the Rectangle class. Rectangle - width : double - height : double + Rectangle(h. Value : int, w. Value : int) : + Rectangle(h. Value : double, w. Value : double) : + get. Area() : double Q: Does the class have overloaded methods? Q: How many Constructors does the Rectangle class have? Q: How many java files are needed? Q: Which fields/methods are public, and which are private CS 110: Programming Fundamentals I, Winter 2017 6 Yes, the constructors 2 2 fields are private, all others are public 9/9/2021

A full class and program (main) example with methods Rectangle - width : double

A full class and program (main) example with methods Rectangle - width : double - height : double // class Rectangle public class Rectangle{ // fields private double height; private double width; + Rectangle(h. Value : int, w. Value : int) : + Rectangle(h. Value : double, w. Value : double) : + get. Area() : double // methods public Rectangle(int h. Value, int w. Value){ height = h. Value; width = w. Value; } Two fields Three methods Two constructors public Rectangle(double h. Value, double w. Value){ height = h. Value; width = w. Value; } Fields of Rectangle are private Methods of Rectangle are public double get. Area(){ return height * width; } CS 110: Programming Fundamentals I, Winter 2017 } 7 9/9/2021

A full class and program (main) example with methods This file is saved as

A full class and program (main) example with methods This file is saved as Rectangle. java Rectangle - width : double - height : double + Rectangle(h. Value : int, w. Value : int) : + Rectangle(h. Value : double, w. Value : double) : + get. Area() : double Even though the field height and width are of type double, they can be assigned integer values because there is no loss of precision … but the values saved in the fields height and width are always of type double Method declarations in the class do NOT use the keyword static. Stay tuned to CS 111 to learn why. CS 110: Programming Fundamentals I, Winter 2017 // class Rectangle public class Rectangle{ // fields private double height; private double width; // methods public Rectangle(int h. Value, int w. Value){ height = h. Value; width = w. Value; } public Rectangle(double h. Value, double w. Value){ height = h. Value; width = w. Value; } public double get. Area(){ return height * width; } 8 9/9/2021

A full class and program (main) example with methods This file is saved as

A full class and program (main) example with methods This file is saved as Rectangle. java This file is saved as My. Rectangle. Program. java import java. util. Scanner; public class my. Rectangle. Program{ // class Rectangle public class Rectangle{ public static void main (String[] args){ // fields private double height; private double width; // Scanner user. Input = new Scanner(System. in); // methods public Rectangle(int h. Value, int w. Value){ height = h. Value; width = w. Value; } // query user for input System. out. print("Width of rectangle 1? : "); double width = user. Input. next. Double(); user. Input. next. Line(); System. out. print("Height of rectangle 1? : "); double height = user. Input. next. Double(); user. Input. next. Line(); Rectangle r 1 = new Rectangle(height, width); System. out. print("Width of rectangle 2? : "); width = user. Input. next. Double(); user. Input. next. Line(); System. out. print("Height of rectangle 2? : "); height = user. Input. next. Double(); user. Input. next. Line(); Rectangle r 2 = new Rectangle(height, width); public Rectangle(double h. Value, double w. Value){ height = h. Value; width = w. Value; } public double get. Area(){ return height * width; } } // use the class's get. Area method to retrieve areas System. out. println("The area of r 1 is: " + r 1. get. Area()); System. out. println("The area of r 2 is: " + r 2. get. Area()); } CS 110: Programming Fundamentals I, Winter 2017 9 9/9/2021

A full class and program (main) example with methods import java. util. Scanner; public

A full class and program (main) example with methods import java. util. Scanner; public class my. Rectangle. Program{ This file is saved as My. Rectangle. Program. java public static void main (String[] args){ // Scanner user. Input = new Scanner(System. in); // query user for input System. out. print("Width of rectangle 1? : "); double width = user. Input. next. Double(); user. Input. next. Line(); System. out. print("Height of rectangle 1? : "); double height = user. Input. next. Double(); user. Input. next. Line(); Rectangle r 1 = new Rectangle(height, width); These are needed to consume the new line characters (when the user pressed <Return> after typing a value) that are still in the Scanner's buffer System. out. print("Width of rectangle 2? : "); width = user. Input. next. Double(); user. Input. next. Line(); System. out. print("Height of rectangle 2? : "); height = user. Input. next. Double(); user. Input. next. Line(); Rectangle r 2 = new Rectangle(height, width); // use the class's get. Area method to retrieve areas System. out. println("The area of r 1 is: " + r 1. get. Area()); System. out. println("The area of r 2 is: " + r 2. get. Area()); CS 110: Programming Fundamentals I, Winter 2017 10 9/9/2021

A full class and program (main) example with methods This file is saved as

A full class and program (main) example with methods This file is saved as My. Rectangle. Program. java import java. util. Scanner; public class my. Rectangle. Program{ public static void main (String[] args){ // Scanner user. Input = new Scanner(System. in); // query user for input System. out. print("Width of rectangle 1? : "); double width = user. Input. next. Double(); user. Input. next. Line(); System. out. print("Height of rectangle 1? : "); double height = user. Input. next. Double(); user. Input. next. Line(); Rectangle r 1 = new Rectangle(height, width); When the constructor is invoked with two arguments of type double, java determines which of the two constructors in the class should be invoked. System. out. print("Width of rectangle 2? : "); width = user. Input. next. Double(); user. Input. next. Line(); System. out. print("Height of rectangle 2? : "); height = user. Input. next. Double(); user. Input. next. Line(); Rectangle r 2 = new Rectangle(height, width); // use the class's get. Area method to retrieve areas System. out. println("The area of r 1 is: " + r 1. get. Area()); System. out. println("The area of r 2 is: " + r 2. get. Area()); CS 110: Programming Fundamentals I, Winter 2017 11 9/9/2021

A full class and program (main) example with methods This file is saved as

A full class and program (main) example with methods This file is saved as My. Rectangle. Program. java import java. util. Scanner; public class my. Rectangle. Program{ public static void main (String[] args){ // Scanner user. Input = new Scanner(System. in); // query user for input System. out. print("Width of rectangle 1? : "); double width = user. Input. next. Double(); user. Input. next. Line(); System. out. print("Height of rectangle 1? : "); double height = user. Input. next. Double(); user. Input. next. Line(); Rectangle r 1 = new Rectangle(height, width); System. out. print("Width of rectangle 2? : "); width = user. Input. next. Double(); user. Input. next. Line(); System. out. print("Height of rectangle 2? : "); height = user. Input. next. Double(); user. Input. next. Line(); Rectangle r 2 = new Rectangle(height, width); When this program is compiled and executed … this line of code creates a reference variable, r 1, which is of type Rectangle, while the use of the new keyword instructs java to create a new object of type Rectangle in memory. The assignment operator = assigns the r 1 reference variable to refer (think of “point”) to the Rectangle object. The same happens for r 2. Assuming user enters 3. 4, 5. 5, 6. 1, 2. 2, then : height width r 1 3. 4 5. 5 height width 6. 1 2. 2 r 2 The get. Area method for each object is invoked, which returns the area of THAT object, using THAT object's width and height field values. // use the class's get. Area method to retrieve areas System. out. println("The area of r 1 is: " + r 1. get. Area()); CS 110: Programming Fundamentals I, Winter 2017 System. out. println("The area of r 2 is: " + r 2. get. Area()); 12 9/9/2021

Homework #5 q q q Is due tonight Book questions (Chapter 5 and 6)

Homework #5 q q q Is due tonight Book questions (Chapter 5 and 6) Single programming task Two java files: Casino. java and Gambler. java Create at least four instances of the Gambler class CS 110: Programming Fundamentals I, Winter 2017 13 9/9/2021

Homework #5 Step 1 : Create a new java file, Gambler. java, with the

Homework #5 Step 1 : Create a new java file, Gambler. java, with the fields and methods described in the UML diagram of the class Gambler. Step 2 : Create a new jave file, Casino. java, in which you write a main method that … q q q q Creates four instances of a Gambler object Sets the winning percentage for each Gambler Sets up Scanner for receiving keyboard input Uses a while loop that prompts user to input how much money each gambler should gamble Invokes the gamble. Another. Round method for each gambler Retrieves the results of each gambler's round (whether won or lost) and outputs to the screen the user's net loss, net gain, etc. Gambler gambler 1 Gambler gambler 2 Gambler gambler 3 gambler 4 while( ){ // prompt user for bet amount // invoke the gamble. Another. Round // method of each object of type // Gambler // Retrieve user's net total, // winnings, etc. , using the getter // methods // Print each gambler's gambling data CS 110: Programming Fundamentals I, Winter 2017 14 } 9/9/2021

Up next … Start of Chapter 7 : Arrays CS 110: Programming Fundamentals I,

Up next … Start of Chapter 7 : Arrays CS 110: Programming Fundamentals I, Winter 2017 15 9/9/2021