Staples are our staple Building upon our solution.

Slides:



Advertisements
Similar presentations
Continuation of chapter 6…. Nested while loop A while loop used within another while loop is called nested while loop. Q. An illustration to generate.
Advertisements

Constructors & An Introduction to Methods. Defining Constructor – Car Example Public class car { String Model; double speed; String colour; { Public Car.
Class template Describing a generic class Instantiating classes that are type- specific version of this generic class Also are called parameterized types.
Methods CSC 171 FALL 2004 LECTURE 3. Methods Variables describe static aspects – “nouns” Methods describe dynamic aspects – “verbs”, “behaviors” – Methods.
 To be able to write larger programs ◦ By breaking them down into smaller parts and passing data between the parts.  To understand the concepts of Methods.
16-Aug-15 Java Puzzlers From the book Java Puzzlers by Joshua Bloch and Neal Gafter.
Do You Understand Methods and Parameters? In this section you will be shown 25 different programs. Most of these programs have some type of error. A few,
Saravanan.G.
College Board A.P. Computer Science A Topics Program Design - Read and understand a problem's description, purpose, and goals; Apply data abstraction.
Week 3 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
Copyright 2008 by Pearson Education Building Java Programs Chapter 8 Lecture 8-2: Constructors and Encapsulation reading: self-checks: #10-17.
Programming Progamz pls. Importance VERY IMPORTANT.
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
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!
Interfaces. –An interface describes a set of methods: no constructors no instance variables –The interface must be implemented by some class. 646 java.
Recursion. Math Review Given the following sequence: a 1 = 1 a n = 2*a n-1 OR a n+1 = 2*a n What are the values of the following? a 2 = a 3 = a 4 =
JAVA Classes Review. Definitions Class – a description of the attributes and behavior of a set of computational objects Constructor – a method that is.
OOP in Java : © W. Milner 2005 : Slide 1 Java and OOP Part 2 – Classes and objects.
RECITATION 4. Classes public class Student { } Data Members public class Student { private String name; public String id; }
Java - Classes JPatterson. What is a class? public class _Alpha { public static void main(String [] args) { } You have been using classes all year – you.
Quizard step-by-step. Start with the questions The Question class is abstract –a Superclass You’ll never create something that’s just a question. It’ll.
Writing Static Methods Up until now, we have been USING (calling) static methods that other people have written. Now, we will start CREATING our own static.
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.
FOR LOOP WALK THROUGH public class NestedFor { public static void main(String [] args) { for (int i = 1; i
Java Programming Final Keyword In Java. final keyword The final keyword in java is used to restrict the user. The final keyword can be used in many context.
Singleton Pattern Presented By:- Navaneet Kumar ise
Files Review For output to a file: –FileOutputStream variable initialized to filename (String) and append/not append (boolean) –PrintWriter variable initialized.
Array Size Arrays use static allocation of space. That is, when the array is created, we must specify the size of the array, e.g., int[] grades = new int[100];
Classes - Intermediate
COME 339 WEEK 1. Example: The Course Class 2 TestCourceRunCourse.
AP Computer Science A – Healdsburg High School 1 Unit 9 - Parameter Passing in Java.
Computer Science A 1. Course plan Introduction to programming Basic concepts of typical programming languages. Tools: compiler, editor, integrated editor,
Methods What is a method? Main Method the main method is where a stand alone Java program normally begins execution common compile error, trying.
Programming in Java Transitioning from Alice. Becomes not myFirstMethod but …. public static void main (String[] arg) { // code for testing classes goes.
A Simple Object Oriented Program public class Simple { public static void main (String [] args) { System.out.println(“howdy”); } System.out is an object.
GC211 Data structure Lecture 3 Sara Alhajjam.
Chapter 2 Clarifications
AKA the birth, life, and death of variables.
Department of Computer Science
using System; namespace Demo01 { class Program
Sum of natural numbers class SumOfNaturalNumbers {
Building Java Programs
Decision statements. - They can use logic to arrive at desired results
An Introduction to Java – Part II
Interfaces and Constructors
Classes & Objects: Examples
AP Java Warm-up Boolean Array.
Unit-2 Objects and Classes
Assignment 7 User Defined Classes Part 2
S.VIGNESH Assistant Professor/CSE, SECE
References, Objects, and Parameter Passing
Recursive GCD Demo public class Euclid {
CS2011 Introduction to Programming I Methods (II)
JAVA Constructors.
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)
Sampath Kumar S Assistant Professor, SECE
References and Objects
class PrintOnetoTen { public static void main(String args[]) {
slides created by Ethan Apter
Java Programming with Multiple Classes
Scope of variables class scopeofvars {
Scope scope: The part of a program where a variable exists. From its declaration to the end of the { } braces A variable declared in a for loop exists.
slides created by Ethan Apter
Methods/Functions.
Methods (a.k.a functions)
CIS 110: Introduction to Computer Programming
CSG2H3 Object Oriented Programming
Introduction to Computer Science and Object-Oriented Programming
class Box { double width; double height; double depth; }
Presentation transcript:

Staples are our staple Building upon our solution

Why did this program work public class StaplerSimulation{ public static void main(String[] args) { Stapler myStapler = new Stapler(); System.out.println( myStapler ); myStapler.fill(); System.out.println( myStapler ); myStapler.punch(); System.out.println( myStapler ); }

A peek at the gang of three public Object clone() { int n = this.getNumberStaples(); Stapler duplicate = new Stapler(n); return duplicate; }

Parameterized mutator public void setNumberStaples(int n) { this.numberStaplesLeft = n; } public static void (String[] args) { Stapler myStapler = new Stapler(); myStapler.setNumberStaples(10); System.out.println(myStapler); }

Specific construction: tailored configuring // Stapler(): specific constructor public Stapler(int n) { this.setNumberStaples(n); } public static void (String[] args) { Stapler myStapler = new Stapler(10); System.out.println(myStapler); }

Object passing Let’s design a method whose parameter is an object variable Consider public void takeStaples(Stapler s) Take staples from s to fill the ‘this’ stapler as much as possible How do we do it?

Object passing Does this satisfy our problem? public void takeStaples(Stapler s) { int n = s.getNumberStaples(); this.addStaples(nl); s.setNumberStaples(0); }

Take a look Does this satisfy our problem public void takeStaples(Stapler s) { int n = s.getNumberStaples(); this.addStaples(nl); s.setNumberStaples(0); } public static void main(String[] args) { Stapler s1 = new Stapler(); Stapler s2 = new Stapler(); s1.fill(). s2.fill(); s1.takeStaples(s2); }

Take a better look public void takeStaples(Stapler s) { int n1 = this.getNumberStaples(); int n2 = s.getNumberStaples(); int total = n1 + n2; n1 = Math.min(this.MAX_NUMBER_STAPLES, total); n2 = total - n1; this.setNumberStaples(n1); s.setNumberStaples(n2); }

Encapsulation Consider Stapler.javaStapler.java Consider another Stapler.javaStapler.java Moral Practice what you preacb