COMP 110: Introduction to Programming Tyler Johnson Mar 16, 2009 MWF 11:00AM-12:15PM Sitterson 014.

Slides:



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

COMP 110: Introduction to Programming Tyler Johnson Feb 18, 2009 MWF 11:00AM-12:15PM Sitterson 014.
COMP 110: Introduction to Programming Tyler Johnson Feb 25, 2009 MWF 11:00AM-12:15PM Sitterson 014.
COMP 110: Introduction to Programming Tyler Johnson Mar 23, 2009 MWF 11:00AM-12:15PM Sitterson 014.
COMP 110: Introduction to Programming Tyler Johnson Apr 20, 2009 MWF 11:00AM-12:15PM Sitterson 014.
COMP 110: Introduction to Programming Tyler Johnson Apr 13, 2009 MWF 11:00AM-12:15PM Sitterson 014.
COMP 110: Introduction to Programming Tyler Johnson January 26, 2009 MWF 11:00AM-12:15PM Sitterson 014.
COMP 110: Introduction to Programming Tyler Johnson Feb 23, 2009 MWF 11:00AM-12:15PM Sitterson 014.
COMP 110: Introduction to Programming Tyler Johnson January 12, 2009 MWF 11:00AM-12:15PM Sitterson 014.
COMP 110: Introduction to Programming Tyler Johnson Mar 25, 2009 MWF 11:00AM-12:15PM Sitterson 014.
COMP 110: Introduction to Programming Tyler Johnson Apr 8, 2009 MWF 11:00AM-12:15PM Sitterson 014.
COMP 110: Introduction to Programming Tyler Johnson Apr 1, 2009 MWF 11:00AM-12:15PM Sitterson 014.
COMP 110: Introduction to Programming Tyler Johnson January 28, 2009 MWF 11:00AM-12:15PM Sitterson 014.
COMP 110: Introduction to Programming
COMP 110: Introduction to Programming Tyler Johnson Mar 2, 2009 MWF 11:00AM-12:15PM Sitterson 014.
COMP 110: Introduction to Programming Tyler Johnson Apr 27, 2009 MWF 11:00AM-12:15PM Sitterson 014.
COMP 110: Introduction to Programming Tyler Johnson Feb 4, 2009 MWF 11:00AM-12:15PM Sitterson 014.
UNIT II DATA TYPES OPERATORS AND CONTROL STATEMENT 1.
2010 Tax Class1 Day 3 Class Participation Class Exercise – Paul Austin Publ W Exercise page 98 Advanced Section.
1 Classes and Objects in Java Basics of Classes in Java.
Final and Abstract Classes
Basic Java Constructs and Data Types – Nuts and Bolts
1 Inheritance Classes and Subclasses Or Extending a Class.
Classes and Objects in Java
Chapter 7 Constructors and Other Tools. Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 7-2 Learning Objectives Constructors Definitions.
Large Scale Integration of Senses for the Semantic Web Jorge Gracia, Mathieu dAquin, Eduardo Mena Computer Science and Systems Engineering Department (DIIS)
Producing monthly estimates of labour market indicators exploiting the longitudinal dimension of the LFS microdata R. Gatto, S. Loriga, A. Spizzichino.
Slides prepared by Rose Williams, Binghamton University ICS201 Lecture 4 : Polymorphism King Fahd University of Petroleum & Minerals College of Computer.
9/09/20091 Risk Analysis Bob Sklar Engineering Fellow & Chief Engineer (Retired) Raytheon Company University of Arizona SIE-554 Fall 2009.
Wouter Noordkamp The assessment of new platforms on operational performance and manning concepts.
Ziehm Academy - User Guide for online registration portal Nuremberg, February 2009.
RADITEK inc RADITEK was founded in 1993
Intro to CS – Honors I More Objects and Methods GEORGIOS PORTOKALIDIS
AŽD Praha s.r.o. CRV&AVV ATO system used on vehicles of Czech Railways Dr.Ing. Ivo Myslivec AŽD Praha, Plant Technics, R&D WG1.
Efficient Solutions For Water Supply Helix High-Pressure Vertical Multistage Pump.
1 Cathay Life Insurance Ltd. (Vietnam) 27/11/20091.
Presented by: Yaseen Ali, Suraj Bhardwaj, Rohan Shah Yaseen Ali, Suraj Bhardwaj, Rohan Shah Mechatronics Engineering Group 302 Instructor: Dr. K. Sartipi.
1 User-Defined Classes The String class is a pre-defined class that is provided by the Java Designer. Sometimes programmers would like to create their.
1 Review Quisioner Kendala: Kurang paham materi. Praktikum Pengaruh teman.
Vault 9 Project Update 9 th September 2009 Paul Pointon – Site Project Delivery Manager LLW Repository Ltd.
30 min Scratch July min intro to Scratch A Quick-and-Dirty approach Leaving lots of exploration for the future. (5 hour lesson plan available)
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 14: More About Classes.
Flexible Scheduling of Software with Logical Execution Time Constraints* Stefan Resmerita and Patricia Derler University of Salzburg, Austria *UC Berkeley,
The Line Class Suppose you are involved in the development of a large mathematical application, and this application needs an object to represent a Line.
Core Java Lecture 4-5. What We Will Cover Today What Are Methods Scope and Life Time of Variables Command Line Arguments Use of static keyword in Java.
Copyright 2008 by Pearson Education Building Java Programs Chapter 8 Lecture 8-3: Encapsulation, toString reading: self-checks: #13-18,
INHERITANCE BASICS Reusability is achieved by INHERITANCE
1 Classes, Encapsulation, Methods and Constructors (Continued) Class definitions Instance data Encapsulation and Java modifiers Method declaration and.
1 Classes Overview l Classes as Types l Declaring Instance Variables l Implementing Methods l Constructors l Accessor and Mutator Methods.
Constructors. Defining Constructors  A constructor is a special kind of method that is designed to perform initializations, such as giving values to.
Classes with multiple methods Part 1. Review of classes & objects Early on, we learned that objects are the basic working units in object-oriented programs.
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!
COMP 110 Constructors Luv Kohli October 13, 2008 MWF 2-2:50 pm Sitterson 014.
10-Nov-15 Java Object Oriented Programming What is it?
JAVA Classes Review. Definitions Class – a description of the attributes and behavior of a set of computational objects Constructor – a method that is.
More About Objects and Methods Chapter 5. Outline Programming with Methods Static Methods and Static Variables Designing Methods Overloading Constructors.
Chapter 3 Introduction to Classes and Objects Definitions Examples.
COMP 110: Introduction to Programming Tyler Johnson Feb 16, 2009 MWF 11:00AM-12:15PM Sitterson 014.
Catie Welsh March 23,  Lab 6 due Friday by 1pm 2.
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.
Classes & Objects: Examples
Classes, Encapsulation, Methods and Constructors (Continued)
Questions? Math Class Wrapper Classes Writing / Testing Methods.
Announcements Program 2 is due tomorrow by noon Lab 4 was due today
Announcements Lab 5 was due today Program 3 due Monday by 12pm
Today in COMP 110 Brief static review The Math Class Wrapper Classes
Classes and Objects CGS3416 Spring 2019.
More About Objects and Methods Chapter 5 Programming with Methods
Presentation transcript:

COMP 110: Introduction to Programming Tyler Johnson Mar 16, 2009 MWF 11:00AM-12:15PM Sitterson 014

COMP 110: Spring Questions?

COMP 110: Spring Program 3 Example solutions

COMP 110: Spring Today in COMP 110 Constructors Static Variables & Methods

COMP 110: Spring Constructors Section 6.1 in text

COMP 110: Spring Creating Objects 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

COMP 110: Spring 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

COMP 110: Spring 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

COMP 110: Spring 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; }

COMP 110: Spring 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

COMP 110: Spring 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

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

COMP 110: Spring 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

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

COMP 110: Spring 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.setPet("Roberto", 1, 150.0); //ok

COMP 110: Spring 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; } 16

COMP 110: Spring Static Variables & Methods Section 6.2 in text

COMP 110: Spring The Keyword Static The keyword static is a specifier that can be applied to instance variables and methods in Java You are already familiar with some other specifiers such as public, private, final public class Student { private double gpa; public double getGPA() { return gpa; }

COMP 110: Spring The Keyword Static The keyword static is used to indicate that only ONE copy of the instance variable or method should exist for the entire class public class UnitsAndMeasures { //static, all objects share the SAME copy of this variable public static final int FEET_PER_YARD = 3; //NOT static, all objects have their OWN copy of this variable private int feet; }

COMP 110: Spring Example: Static Instance Variables A class that counts the number of method calls to ALL of its objects public class StaticExample { //static, all objects share the SAME copy of this variable private static numberOfCalls = 0; public void method() { numberOfCalls++; } public class StaticExampleTester { public static void main(String[] args) { StaticExample se = new StaticExample(); StaticExample se2 = new StaticExample(); se.method(); //changes numberOfCalls to 1 se2.method(); //changes numberOfCalls to 2 }

COMP 110: Spring The Keyword Static The keyword static can also be used with methods The main method public static void main() { StaticExample se = new StaticExample(); StaticExample se2 = new StaticExample(); se.method(); //changes numberOfCalls to 1 se2.method(); //changes numberOfCalls to 2 }

COMP 110: Spring Using the Keyword Static When should you use the keyword static with a method? When a method does not access instance variables public int pow(int x, int y) { int result = 1; for(int i = 0; i < y; i++) { result *= x; } return result; } Does this method access any instance variables? No. Should be declared static

COMP 110: Spring Example public class DimensionConverter { public static final int INCHES_PER_FOOT = 12; public static final double convertFeetToInches(double feet) { return feet*INCHES_PER_FOOT; } public static double convertInchesToFeet(double inches) { return inches / INCHES_PER_FOOT; }

COMP 110: Spring Accessing Static Variables From outside the class, static variables that are declared public can be accessed using the name of the class int inchesPerFoot = DimensionConverter.INCHES_PER_FOOT; Class Name Static Variable Name No Object is Specified!

COMP 110: Spring Calling Static Methods From outside the class, static methods that are declared public can be accessed using the name of the class int inches = DimensionConverter.convertFeetToInches(12); Class Name Static Variable Name No Object is Specified!

COMP 110: Spring Restrictions on Static Methods Static methods CANNOT Access non-static instance variables Call non-static methods Static methods CAN Be called by any method, static or non-static

COMP 110: Spring Restrictions on Static Methods Access Non-Static Variables Access Static Variables Call Non-Static Methods Call Static Methods Static methods XX Non-Static Methods

COMP 110: Spring Example public class Circle { public static final double PI = ; private double area; public static double area(double radius) { area = PI * (radius * radius); return area; } Will this code compile?No. Cannot access non-static instance variables in static methods

COMP 110: Spring Example public class Circle { public static final double PI = ; private void printArea(double area) { System.out.println(area); } public static void area(double radius) { printArea(PI * (radius * radius)); } Will this code compile?No. Cannot call non-static methods inside static methods

COMP 110: Spring Example public class Circle { public static final double PI = ; private void printArea() { System.out.println(area(3.0)); } public static double area(double radius) { return PI * (radius * radius); } Will this code compile?Yes. CAN call static methods inside non-static methods

COMP 110: Spring 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 Fs, compute the percentage of each type of grade e.g. 15% As, 30% Bs, 30% Cs, 15% Ds, 10% Fs Include accessors and mutators for each type of grade Draw a bar graph of the grade distribution

COMP 110: Spring Programming Demo Output Each * == 2 percent | | | | | | | | | | | ************************************************** **** A ************** B *********C *****D ***F

COMP 110: Spring Wednesday Math class Wrapper class Writing & Testing Methods