1301 Review Part 2 More Stuff You Should Know Briana B. Morrison CSE 1302C Spring 2010.

Slides:



Advertisements
Similar presentations
Based on Java Software Development, 5th Ed. By Lewis &Loftus
Advertisements

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.
Topic 10 Java Memory Management. 1-2 Memory Allocation in Java When a program is being executed, separate areas of memory are allocated for each class.
C++ Classes & Data Abstraction
Written by: Dr. JJ Shepherd
CIT 590 Intro to Programming Java lecture 4. Agenda Types Collections – Arrays, ArrayLists, HashMaps Variable scoping Access modifiers – public, private,
Road Map Introduction to object oriented programming. Classes
School of Computing Science CMT1000 © Ed Currie Middlesex University Lecture 9: 1 CMT1000: Introduction to Programming Ed Currie Lecture 11: Objects.
CS 106 Introduction to Computer Science I 03 / 21 / 2008 Instructor: Michael Eckmann.
Evan Korth New York University Computer Science I Classes and Objects Professor: Evan Korth New York University.
Terms and Rules Professor Evan Korth New York University (All rights reserved)
CSE 1301 Lecture 11 Object Oriented Programming Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick.
Writing Classes You have already used classes –String, Random, Scanner, Math, Graphics, etc –To use a class: import the class or the package containing.
BPJ444: Business Programming Using Java Classes and Objects Tim McKenna
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
Spring 2008 Mark Fontenot CSE 1341 Principles of Computer Science I Note Set 2.
1. 2 Reference... Student stu; Reference of Student stu When the reference is created it points to a null value. Before access the reference, objects.
Review of ICS 102. Lecture Objectives To review the major topics covered in ICS 102 course Refresh the memory and get ready for the new adventure of ICS.
ECE122 Feb. 22, Any question on Vehicle sample code?
JAVA Classes Review. Definitions Class – a description of the attributes and behavior of a set of computational objects Constructor – a method that is.
CSC 142 Computer Science II Zhen Jiang West Chester University
Java - Classes JPatterson. What is a class? public class _Alpha { public static void main(String [] args) { } You have been using classes all year – you.
Classes. Constructor A constructor is a special method whose purpose is to construct and initialize objects. Constructor name must be the same as the.
CSE 1301 Lecture 6 Writing Classes Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick.
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.
JAVA PROGRAMMING PART III. METHOD STATEMENT Form of method statement [ ] [static] ( [ ]) { } Example public static void main(String args[])
Java Classes Chapter 1. 2 Chapter Contents Objects and Classes Using Methods in a Java Class References and Aliases Arguments and Parameters Defining.
Method Parameters and Overloading Version 1.0. Topics The run-time stack Pass-by-value Pass-by-reference Method overloading Stub and driver methods.
1 Review for exam 2 CS 101 Spring 2005 Aaron Bloomfield.
Attribute - CIS 1068 Program Design and Abstraction Zhen Jiang CIS Dept. Temple University SERC 347, Main Campus 12/24/2016.
Classes - Intermediate
Methods.
College Board Topics – A.P. Computer Science A Program Design - Read and understand a problem's description, purpose, and goals; Apply data abstraction.
Exam 2 EXAM 2 Thursday!!! 25% of Final Grade Know: loops, switch/case Files Input failure (e.g. scan.hasNextInt())
Today Encapsulation. Build a fully encapsulated Halloween class, going from Halloween1 to Halloween6 (eventually!): –The final version will have overloaded.
C# Programming: From Problem Analysis to Program Design1 Creating Your Own Classes C# Programming: From Problem Analysis to Program Design 4th Edition.
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.
Sadegh Aliakbary Sharif University of Technology Fall 2010.
Java 5 Class Anatomy. User Defined Classes To this point we’ve been using classes that have been defined in the Java standard class library. Creating.
Java: Base Types All information has a type or class designation
Re-Intro to Object Oriented Programming
Java: Base Types All information has a type or class designation
BİL527 – Bilgisayar Programlama I
Intro To Classes Review
Review Session.
Creating Your OwnClasses
CS 302 Week 11 Jim Williams, PhD.
More Object Oriented Programming
Instance Method Review - CIS 1068 Program Design and Abstraction
CSC240 Computer Science III
Lecture 11 C Parameters Richard Gesick.
Classes Variables That Are Not of a Built-in Type Are Objects
Overloading and Overriding
Classes & Objects: Examples
Review of Classes and Arrays
CS 302 Week 9 Jim Williams.
More on Classes and Objects
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)
Basics of OOP A class is the blueprint of an object.
© A+ Computer Science - OOP Pieces © A+ Computer Science -
Building Java Programs
1D Arrays and Lots of Brackets
1D Arrays and Lots of Brackets
Review of Classes and Arrays
CIS 199 Final Review.
1D Arrays and Lots of Brackets
Review of Classes and Arrays
1D Arrays and Lots of Brackets
Presentation transcript:

1301 Review Part 2 More Stuff You Should Know Briana B. Morrison CSE 1302C Spring 2010

2 CSE 1302C Topics Defining classes Using objects

3 CSE 1302C Defining a Class Contain members –Attributes (data) Instance Class (static) –Methods (behaviors) Instance Class (static) Access (public, protected, private)

4 CSE 1302C Trophy Example Define a class that would store the trophy name and points Attributes What methods –Constructor –Properties (accessors / mutators) –Ability to print (overload ToString)

5 CSE 1302C class Trophy { // attributes private string _Name; private int _Points = 10; // default constructor public Trophy() { _Name = "Yeah that's a problem"; _Points = 0; } // overloaded constructor public Trophy (string n, int p) { _Name = n; Points = p; }

6 CSE 1302C class Trophy { // properties public string Name { get { return _Name; } } public int Points { get { return _Points; } set { if (value > 0) _Points = value; }

7 CSE 1302C class Trophy { // overloaded ToString public override string ToString() { return _Name + " (" + _Points + ")"; }

8 CSE 1302C Using a Class Declare and instantiate an instance Print the values of the instance Create a collection (array) of trophy instances Read in values, sum the points in the collection

9 CSE 1302C // declare and instantiate instance Trophy t = new Trophy("100 kills", 10); // print values Console.WriteLine(t);

10 CSE 1302C // create collection Trophy[ ] data_storage; Console.Write("How many trophies: "); int trophie_count; trophie_count = Int32.Parse(Console.ReadLine()); data_storage = new Trophy[trophie_count]; // read in values string name; int points; for (int i = 0; i < trophie_count; i++) { Console.Write("Enter trophy " + i + " name:"); name = Console.ReadLine(); Console.Write("Enter points for this trophy: "); points = Int32.Parse(Console.ReadLine()); data_storage[i] = new Trophy(name, points); }

11 CSE 1302C // print collection and total points int sum = 0; Console.WriteLine("Uber Gamer Prophile"); // for (int i = 0; i < data_storage.Length; i++) // Console.WriteLine(data_storage[i]); // data_storage[0].Points += 9; foreach (Trophy troph in data_storage) Console.WriteLine(troph); foreach (Trophy troph in data_storage) sum += troph.Points; Console.WriteLine("Your points are " + sum);

12 CSE 1302C Another Class Example How about a superhero class –Name –Superhero ability

13 CSE 1302C class Hero { public enum PowerType {Strength, Fly, Speed, MindControl, SprVision}; public string Name; public PowerType SuperPower; } Notice in the above that the enum allows us to define a new "type", but this is actually just a place holder for integers.

14 CSE 1302C class Hero { public void Fight(Hero opponent) { if (this.SuperPower == PowerType.SprVision) { Console.WriteLine(this.Name + " beats up " + opponent.Name); } else { Console.WriteLine(Name + " fights " + opponent.Name); }

15 CSE 1302C Use the Hero Class Create 2 heroes with powers and have them fight.

16 CSE 1302C class Program { static void Main(string[ ] args) { Hero SpiderMan; SpiderMan = new Hero(); SpiderMan.Name = "Peter Parker"; SpiderMan.SuperPower = Hero.PowerType.BusVision; Hero Venom = new Hero(); // Hero Venom; // Venom = null; Venom.Name = "Venom"; Venom.SuperPower = Hero.PowerType.Fly; Venom.Fight(SpiderMan); SpiderMan.Fight(Venom); Console.Write("Press ENTER"); Console.ReadLine(); }

17 CSE 1302C class Program { static void Main(string[] args) { DoArrayStuff(); } // private static void DoArrayStuff() { int[ ] nums = new int[10]; //remember to not forget to use "new" to allocate memory for your object. for (int i = 0; i < 10; i++) { Console.Write("Enter number " + i + " : "); nums[i] = Int32.Parse(Console.ReadLine()); } float average = FindAverage(nums); Console.WriteLine("The average is " + average); int min = FindMin(nums); Console.WriteLine("The min is " + min); }

18 CSE 1302C private static int FindMin(int[] A) { int temp = A[0]; for (int i = 1; i < 10; i++) { if (temp > A[i]) temp = A[i]; } return temp; } // private static float FindAverage(int[] A) { int sum = 0; for (int i = 0; i < 10; i++) { sum += A[i]; } return sum / 10f; } Also note that the parameters (in this case "nums") when you invoke a method can be called something in the method itself (in this case "A"). Parameters match in position and type, so the name doesn't matter.

19 CSE 1302C Questions?