CSc2310 tutoring session, week 8 Fall, 2012 Haidong(Haydon) Xue 5:30pm—8:30pm 11/27/2012 and 11/28/2012 -Class Members vs Instance Members.

Slides:



Advertisements
Similar presentations
Pertemuan 2 Teknik Animasi 2D dan 3D.
Advertisements

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.
1 Classes and Objects in Java Parameter Passing, Delegation, Visibility Control, and Object Cleanup.
The 3 rd and 4 th tutoring session -- A case study about using variables Fall, 2012 Haidong(Haydon) Xue 5:30pm8:30pm 9/11/2012 and 9/12/2012.
INHERITANCE BASICS Reusability is achieved by INHERITANCE
START DEFINITIONS values (3) N1 = (8, 1,-9) i N1 average N3,2 sum N2 = 0 temp N1 Do not guess or assume any values! Follow the values of the variables.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Topics  Applets  Classes used for graphics Graphics Point Dimension.
Slides prepared by Rose Williams, Binghamton University Chapter 19 Swing II.
APPLETS CSC 171 FALL 2004 LECTURE 6. APPLETS Graphical Java programs Run inside web browser Platform-neutral Easy deployment--loads when needed Secure.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 5 Java Graphics Applets.
Writing methods. Calling Methods nameOfMethod(parameters) if method returns something, use that value Math.pow(2, 3) returns 8 System.out.println(Math.pow(2,
Writing Methods. Create the method Methods, like functions, do something They contain the code that performs the job Methods have two parts.
CSc2310 tutoring session, week 8 Fall, 2012 Haidong Xue 5:30pm—8:30pm 10/30/2012 and 10/31/2012 -Code PrintCalendar.
UML Basics & Access Modifier
Hello AP Computer Science!. What are some of the things that you have used computers for?
The 10 th and 11 th tutoring session Fall, 2012 Haidong Xue 5:30pm—8:30pm 10/9/2012 and 10/10/2012 -What is a class, what is an object and why we need.
Methods CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
CSc2310 tutoring session, week 7 Fall, 2012 Haidong Xue 5:30pm—8:30pm 10/9/2012 and 10/10/2012 -Using arrays -Case study: PhoneDirectory.
Applets CS 3331 Sections 3.3 & 4.7 of [Jia03].
Week 3 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
CSc2310 tutoring session, week 8 Fall, 2012 Haidong Xue 5:30pm—8:30pm 10/23/2012 and 10/24/2012 -Using Exceptions -Homework 4.
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.
Chapter 6: Graphics Java Programming FROM THE BEGINNING Copyright © 2000 W. W. Norton & Company. All rights reserved. 1 Chapter 6 Graphics.
Chapter 6: Graphics Java Programming FROM THE BEGINNING Copyright © 2000 W. W. Norton & Company. All rights reserved. 1 Chapter 6 Graphics.
1 CSC 110AA Introduction to Computer Science for Majors - Spring 2003 Class 5 Chapter 2 Type Casting, Characters, and Arithmetic Operators.
Static Class Methods CSIS 3701: Advanced Object Oriented Programming.
CSc2310 tutoring session, week 8 Fall, 2012 Haidong Xue 5:30pm—8:30pm 11/06/2012 and 11/07/2012 -Test 3 Study Guide.
The 5 th and 6 th tutoring session - A case study about verifying a ISBN - Writing code for problems in HW1 Fall, 2012 Haidong(Haydon) Xue 5:30pm—8:30pm.
The 1 st and 2 nd tutoring session of CSc2310 Fall, 2012 Haidong Xue.
CSC Programming I Lecture 6 September 4, 2002.
BallWorld.java A structured walkthrough. Key Features: 2 classes are created Execution is done through the procedure called “main” which are decleared.
BEGINNING PROGRAMMING.  Literally – giving instructions to a computer so that it does what you want  Practically – using a programming language (such.
COP3502 Programming Fundamentals for CIS Majors 1 Instructor: Parisa Rashidi.
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.
Painting (Chapter 12) Java Certification Study Group January 25, 1999 Mark Roth.
The 9 th and 10 th tutoring session Fall, 2012 Haidong Xue 5:30pm—8:30pm 10/2/2012 and 10/3/2012 -Review loop structures and improve CheckISBN and CourseAverage.
BallWorld.java Ball.java A functional walkthrough Part 3: the interaction of objects.
CSc2310 tutoring session, week 8 Fall, 2012 Haidong Xue 5:30pm—8:30pm 11/13/2012 and 11/14/2012 -Dr Cao’s Assignment 5 -i.e. Problem 4 on page 335 and.
Chapter 9 - Classes with Class Members Class Variables Class Methods How to Access Class Members When to Use Class Members Class Constants Example Program.
Interfaces and Inner Classes
Bank Account Example public class BankAccount { private double balance; public static int totalAccounts = 0; public BankAccount() { balance = 0; totalAccounts++;
Catie Welsh March 23,  Lab 6 due Friday by 1pm 2.
Class Variables and Methods
Structured Programming Dr. Atif Alhejali Lecture 4 Modifiers Parameters passing 1Structured Programming.
The 7 th and 8 th tutoring session Fall, 2012 Haidong(Haydon) Xue 5:30pm—8:30pm 9/25/2012 and 9/26/ Review control structures - Improve CheckISBN.
Getting Started with GUI Programming Chapter 10 CSCI 1302.
Problem of the Day  Why are manhole covers round?
Programming in Java Transitioning from Alice. Becomes not myFirstMethod but …. public static void main (String[] arg) { // code for testing classes goes.
Chapter 7: Class Variables and Methods Java Programming FROM THE BEGINNING Copyright © 2000 W. W. Norton & Company. All rights reserved. 1 Class Methods.
Java Applets.
GC211 Data structure Lecture 3 Sara Alhajjam.
9.1 Class (static) Variables and Methods
Examples of Classes & Objects
using System; namespace Demo01 { class Program
Graphics Chapter 6 Copyright © 2000 W. W. Norton & Company.
Haidong Xue Summer 2011, at GSU
USING ECLIPSE TO CREATE HELLO WORLD
The FacebookPerson Example
Computing Adjusted Quiz Total Score
Graphics Chapter 6 Copyright © 2000 W. W. Norton & Company.
Classes & Objects: Examples
The FacebookPerson Example
Java Applets.
CS2011 Introduction to Programming I Methods (II)
Simple Classes in Java CSCI 392 Classes – Part 1.
class PrintOnetoTen { public static void main(String args[]) {
Methods/Functions.
Presentation transcript:

CSc2310 tutoring session, week 8 Fall, 2012 Haidong(Haydon) Xue 5:30pm—8:30pm 11/27/2012 and 11/28/2012 -Class Members vs Instance Members

CSc2310 Tutoring Time: 5:30pm-8:30pm Tutor: Haidong Xue Website: There are 2 sections: 1.Review -Class Members vs Instance Members 2. Q&A -Answer your questions about java programming

Class Members vs Instance Members Members of a class Class Members (i.e. static members) Variable MembersMethod Members Instance Members Variable MembersMethod Members Relations about “members”

Class Members vs Instance Members Grammar To define a “instance member” – Inside a class, define a variable or a method To define a “class member” – Inside a class, put a static modifier before the data type, and define the variable or the method

Members of a class Class Members Variable MembersMethod Members Instance Members Variable MembersMethod Members Example: public class ClassVariableAndInstanceVariable{ private String instance_v; // each object has one private static int class_v=0; // all objects share the same one public ClassVariableAndInstanceVariable(String s ){ instance_v = s; class_v++; } public void instance_method(){ System.out.println(instance_v + "'s instance method is called"); System.out.println("There are " + class_v + " objects of this ClassVariableAndInstanceVariable"); } public static void class_method(){ System.out.println("There are " + class_v + " objects of this ClassVariableAndInstanceVariable"); } Instance methods can access all types of members Static methods can only access static members

Class Members vs Instance Members How to use static members (class members) and instance members? Instance Members – From an object! Class Members – From a class!

Members of a class Class Members Variable MembersMethod Members Instance Members Variable MembersMethod Members Example: public class ClassVariableAndInstanceVariableTester{ public static void main(String[] args){ ClassVariableAndInstanceVariable o1 = new ClassVariableAndInstanceVariable("o1"); ClassVariableAndInstanceVariable.class_method(); ClassVariableAndInstanceVariable o2 = new ClassVariableAndInstanceVariable("o2"); ClassVariableAndInstanceVariable.class_method(); ClassVariableAndInstanceVariable o3 = new ClassVariableAndInstanceVariable("o3"); ClassVariableAndInstanceVariable.class_method(); o1.instance_method(); o2.instance_method(); o3.instance_method(); } Output: There are 1 objects of this ClassVariableAndInstanceVariable There are 2 objects of this ClassVariableAndInstanceVariable There are 3 objects of this ClassVariableAndInstanceVariable o1's instance method is called o2's instance method is called o3's instance method is called

An Example in you class – CourseAverage2 public class DisplayText2{ private static Graphics g; // Class variable public static void main(String[] args){ // Create drawable frame DrawableFrame df = new DrawableFrame("Display Text"); df.show(); df.setSize(210, 85); // Obtain graphics context g = df.getGraphicsContext(); // Display "Monospaced Bold" displayFont(Color.red, "Monospaced", Font.BOLD, "Monospaced Bold", 25); // Display "SansSerif Italic" displayFont(Color.green, "SansSerif", Font.ITALIC, "SansSerif Italic", 50); // Display "Serif Plain" displayFont(Color.blue, "Serif", Font.PLAIN, "Serif Plain", 75); // Repaint frame df.repaint(); } private static void displayFont(Color c, String fontName, int fontStyle, String message, int y){ g.setColor(c); g.setFont(new Font(fontName, fontStyle, 20)); g.drawString(message, 15, y); } It illustrates how to use static members, but it is not a very good example since not too much advantage is obtained by doing so.

An Example in you class – DisplayText2 private static double readProgramScores() { double programTotal = 0.0; for (int i = 1; i <= NUM_PROGRAMS; i++) programTotal += readDouble("Enter Program " + i + " score: "); return programTotal; } private static double readQuizScores() { double quizTotal = 0.0; for (int i = 1; i <= NUM_QUIZZES; i++) quizTotal += readDouble("Enter Quiz " + i + " score: "); return quizTotal; } private static double readDouble(String prompt) { SimpleIO.prompt(prompt); String userInput = SimpleIO.readLine(); return Convert.toDouble(userInput); } // Constants private static final int NUM_PROGRAMS = 8; private static final int NUM_QUIZZES = 5; private static final int MAX_PROG_SCORE = 20; private static final int MAX_QUIZ_SCORE = 10; private static final double PROGRAM_WEIGHT =.30; private static final double QUIZ_WEIGHT =.10; private static final double TEST_WEIGHT =.15; private static final double FINAL_EXAM_WEIGHT =.30; Using static members to finish the task

Now you know how to use static members. However, here is the warning from Haydon: Minimize the number of them, i.e. use them only when there is really a huge advantage. The shared variables (all static variables are shared by all objects) will exponentially increase the time of debugging.

Please let me know your questions. I will be here till 8:30pm