Announcements Lab 5 was due today Program 3 due Monday by 12pm

Slides:



Advertisements
Similar presentations
COMP 110: Introduction to Programming Tyler Johnson Feb 25, 2009 MWF 11:00AM-12:15PM Sitterson 014.
Advertisements

COMP 110: Introduction to Programming Tyler Johnson Mar 16, 2009 MWF 11:00AM-12:15PM Sitterson 014.
Intro to CS – Honors I More Objects and Methods GEORGIOS PORTOKALIDIS
Recitation 09/12/2007 CS 180 Department of Computer Science, Purdue University.
Lecture 4: Objects and Classes Classes Objects Data fields (instance variables) Methods –Instance methods –Static methods –Constructors But first…
COMP 110 Objects and References Tabitha Peck M.S. February 27, 2008 MWF 3-3:50 pm Philips
CS 106 Introduction to Computer Science I 03 / 21 / 2008 Instructor: Michael Eckmann.
Constructors. Defining Constructors  A constructor is a special kind of method that is designed to perform initializations, such as giving values to.
CS31: Introduction to Computer Science I Discussion 1A 5/28/2010 Sungwon Yang
Chapter Objectives You should be able to describe: Object-Based Programming Classes Constructors Examples Common Programming Errors.
© 2003 Walter Savitch These slides are for the exclusive use of students in CSE 11 at UCSD, Winter quarter They may not be copied or used for any.
More C++ Bryce Boe 2013/07/18 CS24, Summer 2013 C.
Chapter 5 - Writing a Problem Domain Class Definition1 Chapter 5 Writing a Problem Domain Class Definition.
Java Quiz Bowl A fun review of the Java you should know from CMPT 201 If you don’t know the answers - this week is for you to study up!
© A+ Computer Science - public Triangle() { setSides(0,0,0); } Constructors are similar to methods. Constructors set the properties.
COMP 110 Constructors Luv Kohli October 13, 2008 MWF 2-2:50 pm Sitterson 014.
Classes: Member Functions and Implementation November 22, 2002 CSE103 - Penn State University Prepared by Doug Hogan.
JAVA Classes Review. Definitions Class – a description of the attributes and behavior of a set of computational objects Constructor – a method that is.
Classes. Student class We are tasked with creating a class for objects that store data about students. We first want to consider what is needed for the.
2 Objectives You should be able to describe: Object-Based Programming Classes Constructors Examples Common Programming Errors.
Classes and Objects CS177 Rec 10. Announcements Project 4 is posted ◦ Milestone due on Nov. 12. ◦ Final submission due on Nov. 19. Exam 2 on Nov. 4 ◦
Catie Welsh February 23,  Lab 4 due on Friday  Lab 5 will be assigned on Friday 2.
Java Classes Chapter 1. 2 Chapter Contents Objects and Classes Using Methods in a Java Class References and Aliases Arguments and Parameters Defining.
Catie Welsh March 23,  Lab 6 due Friday by 1pm 2.
Powerpoint slides from A+ Computer Science Modified by Mr. Smith for his course.
AP Java Ch. 4 Review Question 1  Java methods can return only primitive types (int, double, boolean, etc).
Powerpoint slides from A+ Computer Science Modified by Mr. Smith for his course.
Programming in Java Transitioning from Alice. Becomes not myFirstMethod but …. public static void main (String[] arg) { // code for testing classes goes.
Classes CS 162 (Summer 2009). Parts of a Class Instance Fields Methods.
Defining Your Own Classes II
Andrew(amwallis) Classes!
More About Objects and Methods
Yanal Alahmad Java Workshop Yanal Alahmad
Agenda Warmup AP Exam Review: Litvin A2
More About Objects and Methods
HKCT Java OOP Unit 02 Object Oriented Programming in Java Unit 02 Methods, Classes, and Objects 1.
Methods The real power of an object-oriented programming language takes place when you start to manipulate objects. A method defines an action that allows.
CS 302 Week 11 Jim Williams, PhD.
CS 200 Creating Classes Jim Williams, PhD.
Building Java Programs
COMP 110 Information hiding and encapsulation
An Introduction to Java – Part II
Michele Weigle - COMP 14 - Spr 04 Catie Welsh April 11, 2011
slides created by Ethan Apter
Classes, Encapsulation, Methods and Constructors (Continued)
Implementing Non-Static Features
Announcements Lab 8 Was Due Today Lab 7 Regrade Due Thursday
Implementing Classes Chapter 3.
Questions? Math Class Wrapper Classes Writing / Testing Methods.
slides created by Ethan Apter
© A+ Computer Science - OOP © A+ Computer Science -
Announcements Program 2 is due tomorrow by noon Lab 4 was due today
Class Everything if Java is in a class. The class has a constructor that creates the object. public class ClassName private Field data (instance variables)
Announcements Lab 6 was due today Lab 7 assigned this Friday
Midterm Exam Information
Object-Oriented Programming
Instance Method – CSC142 Computer Science II
CMSC202 Computer Science II for Majors Lecture 07 – Classes and Objects (Continued) Dr. Katherine Gibson Based on slides by Chris Marron at UMBC.
Java Programming Language
CS 200 Creating Classes Jim Williams, PhD.
Classes CS 21a: Introduction to Computing I
Dr. R Z Khan Handout-3 Classes
Agenda Warmup Lesson 2.2 (parameters, etc)
CS 200 Creating Classes Jim Williams, PhD.
Announcements Assignment 4 Due Today Lab 8 Due Wednesday
Classes and Objects CGS3416 Spring 2019.
slides created by Ethan Apter and Marty Stepp
Announcements Lab 5 due Wednesday at noon.
More About Objects and Methods Chapter 5 Programming with Methods
Presentation transcript:

Announcements Lab 5 was due today Program 3 due Monday by 12pm Sample Midterm due Monday in class

Questions? Objects & References

Today in COMP 110 Review end of last lecture Constructors Programming Demo (if time permits) In-Class Exercise

Creating Objects Why does this look like a method call? Student jack = new Student(); Why does this look like a method call? Because it is This is a call to a special method called a constructor

Constructors A constructor is a special method that is called when an object is created using new The purpose of a constructor is to perform initializing actions e.g. initialize the instance variables of an object

Constructors The purpose of a constructor is similar to that of a mutator (setter) Used to set the value of variable(s) However, constructors create an object in addition to initializing it

Constructors The classes you have used up to this point use a constructor created automatically by Java Gives default values to instance variables May not be what you want You can specify how instance variables should be initialized by creating your own constructors

Example: Pet class public class Pet { private String name; private int age; private double weight; public Pet() { name = “No name yet.”; age = 0; weight = 0; } public Pet(String initName, int initAge, double initWeight) { name = initName; age = initAge; weight = initWeight;

Constructors A constructor must have the same name as its class The constructor for the class Pet is Pet() The constructor for the class Student is Student() Constructors do NOT specify a return type Not even void

Constructors w/ Parameters Like methods, constructors can have parameters public Pet(String initName, int initAge, double initWeight) { name = initName; age = initAge; weight = initWeight; }

Default Constructor A constructor that takes no arguments is called a default constructor public Pet() { name = “No name yet.”; age = 0; weight = 0; } Java automatically defines a default constructor if you do not define any constructors

Multiple Constructors You can define multiple constructors All have the same name, but different parameters Group their definitions together

Constructors You cannot call a constructor on an existing object Pet myPet = new Pet(); myPet.Pet("Roberto", 1, 150.0); //error Must use mutators on objects that have already been created myPet.setName("Roberto"); myPet.setAge(1); myPet.setWeight(150.0);

Calling Methods within Constructors Just like calling methods within methods /*constructor*/ public Pet(String initName, int initAge, double initWeight) { setPet(initName, initAge, initWeight); //have the mutator perform the set } /*mutator*/ public void setPet(String newName, int newAge, double newWeight) { name = newName; age = newAge; weight = newWeight; 14

Programming Demo Grade Distribution A class to display the distribution of letter grades in a class Given the number of A,B,C,D, and F’s, compute the percentage of each type of grade e.g. 15% A’s, 30% B’s, 30% C’s, 15% D’s, 10% F’s Include accessors and mutators for each type of grade Draw a bar graph of the grade distribution

Programming Demo Output Each * == 2 percent 0 10 20 30 40 50 60 70 80 90 100 | | | | | | | | | | | ************************************************** **** A ************** B *********C *****D ***F

Friday Recitation No new lab will be posted Get help finishing Program 3 Ask questions from practice midterm If you have submitted the Program and completed the practice midterm, you may: show me your completed practice midterm sign out and enjoy your weekend