SlideShare a Scribd company logo
1 of 30
Creating Your first Class

January

233 Katrin

Slide 1
What do we Need?
• MAIN
• One or more other classes.

January

233 Katrin

Slide 2
What's the Difference (main vs others)?
MAIN CLASS
only one per application

OTHER CLASS(ES)
as many as we need

must contain function
called main

must NOT contain
function called main*

all functions & "globals"
must be static

use static only when we
need class (as opposed
to object) members

NEVER gets instantiated
main called implicitly by
run-time system

- depends functions explicitly called
as necessary
* for now

January

233 Katrin

Slide 3
What's the Difference
(primitive types vs arrays & classes)?
Primitive Types

Arrays & Classes

automatically allocated

dynamically allocated

space for them created space for a reference made
when they are declared
when they are declared
int i;
String S;
that space set when
initialized
int i = 5;

new object created when
initialized
String S = "Cordie";

- NA -

new instance created with
new

January

233 Katrin

Slide 4
Java has ONLY pass by value:
int i

413

F(i);
String S

413
public void F( int other) {
other = 17;
}

↢
F(S);

String: Cordie

int other
413
17

↢
public void F( String other) {
other = "Fred";
}

↢

String: Fred

January

233 Katrin

Slide 5
public class MyInt {
int v;
public set(int n) {
v = n; }
}

F(i);
MyInt i;

But......

↢

MyInt: v: 247
413

January

↢
public void F( MyInt other) {
other .set( 247 );
}
v = n;

233 Katrin

Slide 6
Back to classes......

January

233 Katrin

Slide 7
What's in a (main) Class? [ MAIN.java ]
class MAIN {

// "wrapper"

//Attributes:
static int i;
static String S;
// Methods:
static int f1() { return 0; }

public static void main( String[] args) {
}
} // end MAIN

January

233 Katrin

Slide 8
What's in a (other) Class? [ Other.java ]
class Other {

// "wrapper"

//Attributes:
int i;
String S;

// Methods:
public Other () { // CONSTRUCTOR
}
public String junk() { return "Hi! Y'all!";
}
} // end Other

January

233 Katrin

Slide 9
Access
• global within the class
• local within the class
• public

public int i;

int i;

void F() {
int i;
}

• private
int i;

January

233 Katrin

Slide 10
Classes can "own" (have)
• primitive variables (like int, boolean)
• references (like String S;)
Can an object OWN another object?
(is that slavery?)
not really, not in Java, although....
If I'm the only one who "knows" this object,
I can be said to own it. (there's a difference between
knowing, owning, and being)

January

233 Katrin

Slide 11
Kinds of
Methods / Messages
• access / get / status
– see a private member

• modify / set
– change a private member

• forwarding
– call a member's function

January

233 Katrin

Slide 12
Creating a Class
• decide what the class should KNOW
– these become the attributes (data
members)

• decide what the class should Do
– these become the behaviours (methods /
functions / operations)

January

233 Katrin

Slide 13
Account Class
Questions to ask:
• What does an object of this sort need
to remember (what are its attributes)?
•
•
•
•

current balance [type?]
overdraft limit [type?]
account name [type?]
is account open? [type?] "status"

January

233 Katrin

Slide 14
Account Class
Questions to ask:
• What does an object of this sort need
to do (what are its behaviours)?
– access (the "Gets")
– open / close this account
– print (display) the balance
– accept a deposit
– process a withdrawal

January

233 Katrin

Slide 15
The "Gets"
•
•
•
•

Balance
Limit
Name
isOpen

January

233 Katrin

Slide 16
Open & Close
• change the "status"
to open: just change status
to close: must be empty first

January

233 Katrin

Slide 17
print
• do some output

January

233 Katrin

Slide 18
accept a deposit
•
•
•
•

make sure we're open
ask user for it
check value of deposit
add to balance

January

233 Katrin

Slide 19
process a withdrawal
• make sure we're open
• ask user for amount
• check value of withdrawal
– (check for overdraft)

• subtract from balance

January

233 Katrin

Slide 20
Do a transfer?
• How is this different from a deposit /
withdrawal?
• We only ask user for one amount...
• treat one side as a withdrawal ('cause
we already have the code for that)
• treat the other side as a deposit but we
shouldn't ask the user for the amount,
so we need another function
January

233 Katrin

Slide 21
What about constructors?
public Account (int ident)
{
id = ident;
}
public Account (int ident, int OD)
{
id = ident;
overdraft = OD;
}

TWO?

January

233 Katrin

Slide 22
What does that leave for the main class?
• in main function:
// declare an array for 5 accounts
Account[] list = new Account[MAX];
// fill the array with the actual accounts
for (int i = 0; i < MAX; i++)
list[i] = new Account(i);

January

233 Katrin

Slide 23
Figuring out which Account:

acctno = chooseAccount(NEW, list);
//or
acctno = chooseAccount(OLD, list);
• this will need it's own little menu

January

233 Katrin

Slide 24
once we have an account:
case 'w' :
System.out.print
("Doing Withdrawal....n");
amount = list[acctno].doWithdrawal();
break;
case 'o' :
System.out.print("New Account.n");
list[acctno].openAcct();
break;

January

233 Katrin

Slide 25
Doing a transfer - 1
System.out.print
("Account to transfer FROM:n");
acctfrom = chooseAccount(OLD, list);
System.out.print
("Account to transfer INTO:n");
acctto = chooseAccount(OLD, list);

January

233 Katrin

Slide 26
Doing a transfer - 2
amount = list[acctfrom].doWithdrawal();
if (amount!= 0.0)
result =
list[acctto].doTransfer(amount);
if (result != 0.0)
System.out.print
("Transfer Successful.nn");
else
/* didn't work... put it back */
list[acctfrom].doTransfer(amount);

January

233 Katrin

Slide 27
Putting it together: the Account:
public class Account
{
// the vars
// the constructor(s)
// the methods
}

January

233 Katrin

Slide 28
Putting it together: the Bank:
public class Bank
{
// the static vars
// the static methods
public static void main(String[] args)
{ Account list[] = new Account[MAX];
for (int i = 0; i < MAX; i++)
list[i] = new Account(i);
// do stuff with them
}// end main
}// end Bank

January

233 Katrin

Slide 29
Bank Class

The Classes:

chooseAccount
doTransfer

main

Account Class
balance

limit

open?

name

Account(s)
the "Gets"
doTransfer
openAccount
closeAccount
showBalance

January

233 Katrin

doDeposit
doWithdrawal
Slide 30

More Related Content

What's hot

Module 6 : Essentials of Object Oriented Programming
Module 6 : Essentials of Object Oriented ProgrammingModule 6 : Essentials of Object Oriented Programming
Module 6 : Essentials of Object Oriented ProgrammingPrem Kumar Badri
 
Object and Classes in Java
Object and Classes in JavaObject and Classes in Java
Object and Classes in Javabackdoor
 
03 Object Relational Mapping
03 Object Relational Mapping03 Object Relational Mapping
03 Object Relational MappingRanjan Kumar
 
Class and object in c++
Class and object in c++Class and object in c++
Class and object in c++NainaKhan28
 
Statics in java | Constructors | Exceptions in Java | String in java| class 3
Statics in java | Constructors | Exceptions in Java | String in java| class 3Statics in java | Constructors | Exceptions in Java | String in java| class 3
Statics in java | Constructors | Exceptions in Java | String in java| class 3Sagar Verma
 
Core Java Programming | Data Type | operator | java Control Flow| Class 2
Core Java Programming | Data Type | operator | java Control Flow| Class 2Core Java Programming | Data Type | operator | java Control Flow| Class 2
Core Java Programming | Data Type | operator | java Control Flow| Class 2Sagar Verma
 
Classes and objects
Classes and objectsClasses and objects
Classes and objectsAnil Kumar
 
Java Constructors | Java Course
Java Constructors | Java CourseJava Constructors | Java Course
Java Constructors | Java CourseRAKESH P
 
Unit 1 of java part 2 basic introduction
Unit 1 of java part 2 basic introduction Unit 1 of java part 2 basic introduction
Unit 1 of java part 2 basic introduction AKR Education
 
Java Constructors
Java ConstructorsJava Constructors
Java ConstructorsSaumya Som
 
How to write you first class in c++ object oriented programming
How to write you first class in c++ object oriented programmingHow to write you first class in c++ object oriented programming
How to write you first class in c++ object oriented programmingSyed Faizan Hassan
 
OOPS in java | Super and this Keyword | Memory Management in java | pacakages...
OOPS in java | Super and this Keyword | Memory Management in java | pacakages...OOPS in java | Super and this Keyword | Memory Management in java | pacakages...
OOPS in java | Super and this Keyword | Memory Management in java | pacakages...Sagar Verma
 
Chapter 01 Introduction to Java by Tushar B Kute
Chapter 01 Introduction to Java by Tushar B KuteChapter 01 Introduction to Java by Tushar B Kute
Chapter 01 Introduction to Java by Tushar B KuteTushar B Kute
 

What's hot (20)

class c++
class c++class c++
class c++
 
Module 6 : Essentials of Object Oriented Programming
Module 6 : Essentials of Object Oriented ProgrammingModule 6 : Essentials of Object Oriented Programming
Module 6 : Essentials of Object Oriented Programming
 
Euclideus_Language
Euclideus_LanguageEuclideus_Language
Euclideus_Language
 
Object and Classes in Java
Object and Classes in JavaObject and Classes in Java
Object and Classes in Java
 
03 Object Relational Mapping
03 Object Relational Mapping03 Object Relational Mapping
03 Object Relational Mapping
 
Classes objects in java
Classes objects in javaClasses objects in java
Classes objects in java
 
Constructor & destructor
Constructor & destructorConstructor & destructor
Constructor & destructor
 
Class and object in c++
Class and object in c++Class and object in c++
Class and object in c++
 
Statics in java | Constructors | Exceptions in Java | String in java| class 3
Statics in java | Constructors | Exceptions in Java | String in java| class 3Statics in java | Constructors | Exceptions in Java | String in java| class 3
Statics in java | Constructors | Exceptions in Java | String in java| class 3
 
C# Types of classes
C# Types of classesC# Types of classes
C# Types of classes
 
C# classes objects
C#  classes objectsC#  classes objects
C# classes objects
 
Core Java Programming | Data Type | operator | java Control Flow| Class 2
Core Java Programming | Data Type | operator | java Control Flow| Class 2Core Java Programming | Data Type | operator | java Control Flow| Class 2
Core Java Programming | Data Type | operator | java Control Flow| Class 2
 
Classes and objects
Classes and objectsClasses and objects
Classes and objects
 
Java Constructors | Java Course
Java Constructors | Java CourseJava Constructors | Java Course
Java Constructors | Java Course
 
Java Notes
Java NotesJava Notes
Java Notes
 
Unit 1 of java part 2 basic introduction
Unit 1 of java part 2 basic introduction Unit 1 of java part 2 basic introduction
Unit 1 of java part 2 basic introduction
 
Java Constructors
Java ConstructorsJava Constructors
Java Constructors
 
How to write you first class in c++ object oriented programming
How to write you first class in c++ object oriented programmingHow to write you first class in c++ object oriented programming
How to write you first class in c++ object oriented programming
 
OOPS in java | Super and this Keyword | Memory Management in java | pacakages...
OOPS in java | Super and this Keyword | Memory Management in java | pacakages...OOPS in java | Super and this Keyword | Memory Management in java | pacakages...
OOPS in java | Super and this Keyword | Memory Management in java | pacakages...
 
Chapter 01 Introduction to Java by Tushar B Kute
Chapter 01 Introduction to Java by Tushar B KuteChapter 01 Introduction to Java by Tushar B Kute
Chapter 01 Introduction to Java by Tushar B Kute
 

Viewers also liked

How Do Researchers Choose Commercial Games for Study?
How Do Researchers Choose Commercial Games for Study?How Do Researchers Choose Commercial Games for Study?
How Do Researchers Choose Commercial Games for Study?Katrin Becker
 
Digital Game Based Learning Once Removed
Digital Game Based Learning Once RemovedDigital Game Based Learning Once Removed
Digital Game Based Learning Once RemovedKatrin Becker
 
A Magic Bullet for Educational Games
A Magic Bullet for Educational GamesA Magic Bullet for Educational Games
A Magic Bullet for Educational GamesKatrin Becker
 
Using cards games as learning objects to teach genetics
Using cards games as learning objects to teach geneticsUsing cards games as learning objects to teach genetics
Using cards games as learning objects to teach geneticsKatrin Becker
 
Games For Learning Are Schools Ready for What’s to Come? [DiGRA 2005]
Games For Learning Are Schools Ready for What’s to Come? [DiGRA 2005]Games For Learning Are Schools Ready for What’s to Come? [DiGRA 2005]
Games For Learning Are Schools Ready for What’s to Come? [DiGRA 2005]Katrin Becker
 
How Are Games Educational [DiGRA 2005]
How Are Games Educational [DiGRA 2005]How Are Games Educational [DiGRA 2005]
How Are Games Educational [DiGRA 2005]Katrin Becker
 

Viewers also liked (6)

How Do Researchers Choose Commercial Games for Study?
How Do Researchers Choose Commercial Games for Study?How Do Researchers Choose Commercial Games for Study?
How Do Researchers Choose Commercial Games for Study?
 
Digital Game Based Learning Once Removed
Digital Game Based Learning Once RemovedDigital Game Based Learning Once Removed
Digital Game Based Learning Once Removed
 
A Magic Bullet for Educational Games
A Magic Bullet for Educational GamesA Magic Bullet for Educational Games
A Magic Bullet for Educational Games
 
Using cards games as learning objects to teach genetics
Using cards games as learning objects to teach geneticsUsing cards games as learning objects to teach genetics
Using cards games as learning objects to teach genetics
 
Games For Learning Are Schools Ready for What’s to Come? [DiGRA 2005]
Games For Learning Are Schools Ready for What’s to Come? [DiGRA 2005]Games For Learning Are Schools Ready for What’s to Come? [DiGRA 2005]
Games For Learning Are Schools Ready for What’s to Come? [DiGRA 2005]
 
How Are Games Educational [DiGRA 2005]
How Are Games Educational [DiGRA 2005]How Are Games Educational [DiGRA 2005]
How Are Games Educational [DiGRA 2005]
 

Similar to CS Lesson: Creating Your First Class in Java

Presentation on class and object in Object Oriented programming.
Presentation on class and object in Object Oriented programming.Presentation on class and object in Object Oriented programming.
Presentation on class and object in Object Oriented programming.Enam Khan
 
Object and class presentation
Object and class presentationObject and class presentation
Object and class presentationnafisa rahman
 
Object Oriented Programming Constructors & Destructors
Object Oriented Programming  Constructors &  DestructorsObject Oriented Programming  Constructors &  Destructors
Object Oriented Programming Constructors & Destructorsanitashinde33
 
Unit vi(dsc++)
Unit vi(dsc++)Unit vi(dsc++)
Unit vi(dsc++)Durga Devi
 
APL-2-classes and objects.ppt
APL-2-classes and objects.pptAPL-2-classes and objects.ppt
APL-2-classes and objects.pptsrividyal2
 
Ccourse 140618093931-phpapp02
Ccourse 140618093931-phpapp02Ccourse 140618093931-phpapp02
Ccourse 140618093931-phpapp02Getachew Ganfur
 
C++ Programming Course
C++ Programming CourseC++ Programming Course
C++ Programming CourseDennis Chang
 
Classes, objects and methods
Classes, objects and methodsClasses, objects and methods
Classes, objects and methodsfarhan amjad
 
B.sc CSIT 2nd semester C++ Unit6
B.sc CSIT  2nd semester C++ Unit6B.sc CSIT  2nd semester C++ Unit6
B.sc CSIT 2nd semester C++ Unit6Tekendra Nath Yogi
 
static members in object oriented program.pptx
static members in object oriented program.pptxstatic members in object oriented program.pptx
static members in object oriented program.pptxurvashipundir04
 
Constructor & destructor
Constructor & destructorConstructor & destructor
Constructor & destructorSwarup Boro
 
C++ training
C++ training C++ training
C++ training PL Sharma
 
Connect.Tech- Swift Memory Management
Connect.Tech- Swift Memory ManagementConnect.Tech- Swift Memory Management
Connect.Tech- Swift Memory Managementstable|kernel
 
Object oriented programming in C++
Object oriented programming in C++Object oriented programming in C++
Object oriented programming in C++jehan1987
 

Similar to CS Lesson: Creating Your First Class in Java (20)

Classes and objects
Classes and objectsClasses and objects
Classes and objects
 
Class and objects
Class and objectsClass and objects
Class and objects
 
Presentation on class and object in Object Oriented programming.
Presentation on class and object in Object Oriented programming.Presentation on class and object in Object Oriented programming.
Presentation on class and object in Object Oriented programming.
 
Object and class presentation
Object and class presentationObject and class presentation
Object and class presentation
 
Object Oriented Programming Constructors & Destructors
Object Oriented Programming  Constructors &  DestructorsObject Oriented Programming  Constructors &  Destructors
Object Oriented Programming Constructors & Destructors
 
Unit vi(dsc++)
Unit vi(dsc++)Unit vi(dsc++)
Unit vi(dsc++)
 
APL-2-classes and objects.ppt
APL-2-classes and objects.pptAPL-2-classes and objects.ppt
APL-2-classes and objects.ppt
 
Class and object C++.pptx
Class and object C++.pptxClass and object C++.pptx
Class and object C++.pptx
 
Ccourse 140618093931-phpapp02
Ccourse 140618093931-phpapp02Ccourse 140618093931-phpapp02
Ccourse 140618093931-phpapp02
 
C++ Programming Course
C++ Programming CourseC++ Programming Course
C++ Programming Course
 
Classes, objects and methods
Classes, objects and methodsClasses, objects and methods
Classes, objects and methods
 
oopm 2.pdf
oopm 2.pdfoopm 2.pdf
oopm 2.pdf
 
Unit ii
Unit iiUnit ii
Unit ii
 
B.sc CSIT 2nd semester C++ Unit6
B.sc CSIT  2nd semester C++ Unit6B.sc CSIT  2nd semester C++ Unit6
B.sc CSIT 2nd semester C++ Unit6
 
static members in object oriented program.pptx
static members in object oriented program.pptxstatic members in object oriented program.pptx
static members in object oriented program.pptx
 
Constructor & destructor
Constructor & destructorConstructor & destructor
Constructor & destructor
 
C++ training
C++ training C++ training
C++ training
 
Connect.Tech- Swift Memory Management
Connect.Tech- Swift Memory ManagementConnect.Tech- Swift Memory Management
Connect.Tech- Swift Memory Management
 
Object oriented programming in C++
Object oriented programming in C++Object oriented programming in C++
Object oriented programming in C++
 
Session 09 - OOPS
Session 09 - OOPSSession 09 - OOPS
Session 09 - OOPS
 

More from Katrin Becker

Cross breeding animation
Cross breeding animationCross breeding animation
Cross breeding animationKatrin Becker
 
Assignments that Meet the Needs of Exceptional Students without Disadvantagin...
Assignments that Meet the Needs of Exceptional Students without Disadvantagin...Assignments that Meet the Needs of Exceptional Students without Disadvantagin...
Assignments that Meet the Needs of Exceptional Students without Disadvantagin...Katrin Becker
 
T.A.P. : The Teach Aloud Protocol
T.A.P. : The Teach Aloud ProtocolT.A.P. : The Teach Aloud Protocol
T.A.P. : The Teach Aloud ProtocolKatrin Becker
 
Misguided illusions of understanding
Misguided illusions of understandingMisguided illusions of understanding
Misguided illusions of understandingKatrin Becker
 
4 Pillars of DGBL: A Structured Rating System for Games for Learning
4 Pillars of DGBL: A Structured Rating System for Games for Learning4 Pillars of DGBL: A Structured Rating System for Games for Learning
4 Pillars of DGBL: A Structured Rating System for Games for LearningKatrin Becker
 
Gamification paradigm
Gamification paradigmGamification paradigm
Gamification paradigmKatrin Becker
 
The Calm and The Storm: Simulation and Games - Why All Games are Simulations ...
The Calm and The Storm: Simulation and Games - Why All Games are Simulations ...The Calm and The Storm: Simulation and Games - Why All Games are Simulations ...
The Calm and The Storm: Simulation and Games - Why All Games are Simulations ...Katrin Becker
 
Gamification how to gamify learning and instruction Part 1 (of 3)
Gamification how to gamify learning and instruction Part 1 (of 3)Gamification how to gamify learning and instruction Part 1 (of 3)
Gamification how to gamify learning and instruction Part 1 (of 3)Katrin Becker
 
Gamification how to gamify learning and instruction, part 2 (of 3)
Gamification how to gamify learning and instruction, part 2 (of 3)Gamification how to gamify learning and instruction, part 2 (of 3)
Gamification how to gamify learning and instruction, part 2 (of 3)Katrin Becker
 
Is gamification a game changer
Is gamification a game changerIs gamification a game changer
Is gamification a game changerKatrin Becker
 
CS Example: Parsing a Sentence
CS Example: Parsing a Sentence CS Example: Parsing a Sentence
CS Example: Parsing a Sentence Katrin Becker
 
CS Lesson: Introduction to the Java virtual Machine
CS Lesson: Introduction to the Java virtual MachineCS Lesson: Introduction to the Java virtual Machine
CS Lesson: Introduction to the Java virtual MachineKatrin Becker
 
CS: Introduction to Record Manipulation & Indexing
CS: Introduction to Record Manipulation & IndexingCS: Introduction to Record Manipulation & Indexing
CS: Introduction to Record Manipulation & IndexingKatrin Becker
 
Informing pedagogy through collaborative inquiry
Informing pedagogy through collaborative inquiryInforming pedagogy through collaborative inquiry
Informing pedagogy through collaborative inquiryKatrin Becker
 
Informing SoTL using playtesting techniques
Informing SoTL using playtesting techniquesInforming SoTL using playtesting techniques
Informing SoTL using playtesting techniquesKatrin Becker
 
Gamification how to gamify learning and instruction, Part 3 (of 3)
Gamification how to gamify learning and instruction, Part 3 (of 3)Gamification how to gamify learning and instruction, Part 3 (of 3)
Gamification how to gamify learning and instruction, Part 3 (of 3)Katrin Becker
 
The decorative media trap
The decorative media trapThe decorative media trap
The decorative media trapKatrin Becker
 
When Games and Instructional Design Collide
When Games and Instructional Design CollideWhen Games and Instructional Design Collide
When Games and Instructional Design CollideKatrin Becker
 

More from Katrin Becker (20)

Cross breeding animation
Cross breeding animationCross breeding animation
Cross breeding animation
 
Assignments that Meet the Needs of Exceptional Students without Disadvantagin...
Assignments that Meet the Needs of Exceptional Students without Disadvantagin...Assignments that Meet the Needs of Exceptional Students without Disadvantagin...
Assignments that Meet the Needs of Exceptional Students without Disadvantagin...
 
T.A.P. : The Teach Aloud Protocol
T.A.P. : The Teach Aloud ProtocolT.A.P. : The Teach Aloud Protocol
T.A.P. : The Teach Aloud Protocol
 
Misguided illusions of understanding
Misguided illusions of understandingMisguided illusions of understanding
Misguided illusions of understanding
 
Signature pedagogy
Signature pedagogySignature pedagogy
Signature pedagogy
 
Virtue of Failure
Virtue of FailureVirtue of Failure
Virtue of Failure
 
4 Pillars of DGBL: A Structured Rating System for Games for Learning
4 Pillars of DGBL: A Structured Rating System for Games for Learning4 Pillars of DGBL: A Structured Rating System for Games for Learning
4 Pillars of DGBL: A Structured Rating System for Games for Learning
 
Gamification paradigm
Gamification paradigmGamification paradigm
Gamification paradigm
 
The Calm and The Storm: Simulation and Games - Why All Games are Simulations ...
The Calm and The Storm: Simulation and Games - Why All Games are Simulations ...The Calm and The Storm: Simulation and Games - Why All Games are Simulations ...
The Calm and The Storm: Simulation and Games - Why All Games are Simulations ...
 
Gamification how to gamify learning and instruction Part 1 (of 3)
Gamification how to gamify learning and instruction Part 1 (of 3)Gamification how to gamify learning and instruction Part 1 (of 3)
Gamification how to gamify learning and instruction Part 1 (of 3)
 
Gamification how to gamify learning and instruction, part 2 (of 3)
Gamification how to gamify learning and instruction, part 2 (of 3)Gamification how to gamify learning and instruction, part 2 (of 3)
Gamification how to gamify learning and instruction, part 2 (of 3)
 
Is gamification a game changer
Is gamification a game changerIs gamification a game changer
Is gamification a game changer
 
CS Example: Parsing a Sentence
CS Example: Parsing a Sentence CS Example: Parsing a Sentence
CS Example: Parsing a Sentence
 
CS Lesson: Introduction to the Java virtual Machine
CS Lesson: Introduction to the Java virtual MachineCS Lesson: Introduction to the Java virtual Machine
CS Lesson: Introduction to the Java virtual Machine
 
CS: Introduction to Record Manipulation & Indexing
CS: Introduction to Record Manipulation & IndexingCS: Introduction to Record Manipulation & Indexing
CS: Introduction to Record Manipulation & Indexing
 
Informing pedagogy through collaborative inquiry
Informing pedagogy through collaborative inquiryInforming pedagogy through collaborative inquiry
Informing pedagogy through collaborative inquiry
 
Informing SoTL using playtesting techniques
Informing SoTL using playtesting techniquesInforming SoTL using playtesting techniques
Informing SoTL using playtesting techniques
 
Gamification how to gamify learning and instruction, Part 3 (of 3)
Gamification how to gamify learning and instruction, Part 3 (of 3)Gamification how to gamify learning and instruction, Part 3 (of 3)
Gamification how to gamify learning and instruction, Part 3 (of 3)
 
The decorative media trap
The decorative media trapThe decorative media trap
The decorative media trap
 
When Games and Instructional Design Collide
When Games and Instructional Design CollideWhen Games and Instructional Design Collide
When Games and Instructional Design Collide
 

Recently uploaded

Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slidespraypatel2
 
How to Remove Document Management Hurdles with X-Docs?
How to Remove Document Management Hurdles with X-Docs?How to Remove Document Management Hurdles with X-Docs?
How to Remove Document Management Hurdles with X-Docs?XfilesPro
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationRidwan Fadjar
 
Azure Monitor & Application Insight to monitor Infrastructure & Application
Azure Monitor & Application Insight to monitor Infrastructure & ApplicationAzure Monitor & Application Insight to monitor Infrastructure & Application
Azure Monitor & Application Insight to monitor Infrastructure & ApplicationAndikSusilo4
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024Scott Keck-Warren
 
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024BookNet Canada
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsEnterprise Knowledge
 
Next-generation AAM aircraft unveiled by Supernal, S-A2
Next-generation AAM aircraft unveiled by Supernal, S-A2Next-generation AAM aircraft unveiled by Supernal, S-A2
Next-generation AAM aircraft unveiled by Supernal, S-A2Hyundai Motor Group
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonetsnaman860154
 
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...HostedbyConfluent
 
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | DelhiFULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhisoniya singh
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking MenDelhi Call girls
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationSafe Software
 
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...shyamraj55
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticscarlostorres15106
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machinePadma Pradeep
 
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 3652toLead Limited
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreternaman860154
 

Recently uploaded (20)

Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slides
 
How to Remove Document Management Hurdles with X-Docs?
How to Remove Document Management Hurdles with X-Docs?How to Remove Document Management Hurdles with X-Docs?
How to Remove Document Management Hurdles with X-Docs?
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 Presentation
 
Azure Monitor & Application Insight to monitor Infrastructure & Application
Azure Monitor & Application Insight to monitor Infrastructure & ApplicationAzure Monitor & Application Insight to monitor Infrastructure & Application
Azure Monitor & Application Insight to monitor Infrastructure & Application
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024
 
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
 
Next-generation AAM aircraft unveiled by Supernal, S-A2
Next-generation AAM aircraft unveiled by Supernal, S-A2Next-generation AAM aircraft unveiled by Supernal, S-A2
Next-generation AAM aircraft unveiled by Supernal, S-A2
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
 
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | DelhiFULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
 
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptxE-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
 
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machine
 
The transition to renewables in India.pdf
The transition to renewables in India.pdfThe transition to renewables in India.pdf
The transition to renewables in India.pdf
 
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 

CS Lesson: Creating Your First Class in Java

  • 1. Creating Your first Class January 233 Katrin Slide 1
  • 2. What do we Need? • MAIN • One or more other classes. January 233 Katrin Slide 2
  • 3. What's the Difference (main vs others)? MAIN CLASS only one per application OTHER CLASS(ES) as many as we need must contain function called main must NOT contain function called main* all functions & "globals" must be static use static only when we need class (as opposed to object) members NEVER gets instantiated main called implicitly by run-time system - depends functions explicitly called as necessary * for now January 233 Katrin Slide 3
  • 4. What's the Difference (primitive types vs arrays & classes)? Primitive Types Arrays & Classes automatically allocated dynamically allocated space for them created space for a reference made when they are declared when they are declared int i; String S; that space set when initialized int i = 5; new object created when initialized String S = "Cordie"; - NA - new instance created with new January 233 Katrin Slide 4
  • 5. Java has ONLY pass by value: int i 413 F(i); String S 413 public void F( int other) { other = 17; } ↢ F(S); String: Cordie int other 413 17 ↢ public void F( String other) { other = "Fred"; } ↢ String: Fred January 233 Katrin Slide 5
  • 6. public class MyInt { int v; public set(int n) { v = n; } } F(i); MyInt i; But...... ↢ MyInt: v: 247 413 January ↢ public void F( MyInt other) { other .set( 247 ); } v = n; 233 Katrin Slide 6
  • 8. What's in a (main) Class? [ MAIN.java ] class MAIN { // "wrapper" //Attributes: static int i; static String S; // Methods: static int f1() { return 0; } public static void main( String[] args) { } } // end MAIN January 233 Katrin Slide 8
  • 9. What's in a (other) Class? [ Other.java ] class Other { // "wrapper" //Attributes: int i; String S; // Methods: public Other () { // CONSTRUCTOR } public String junk() { return "Hi! Y'all!"; } } // end Other January 233 Katrin Slide 9
  • 10. Access • global within the class • local within the class • public public int i; int i; void F() { int i; } • private int i; January 233 Katrin Slide 10
  • 11. Classes can "own" (have) • primitive variables (like int, boolean) • references (like String S;) Can an object OWN another object? (is that slavery?) not really, not in Java, although.... If I'm the only one who "knows" this object, I can be said to own it. (there's a difference between knowing, owning, and being) January 233 Katrin Slide 11
  • 12. Kinds of Methods / Messages • access / get / status – see a private member • modify / set – change a private member • forwarding – call a member's function January 233 Katrin Slide 12
  • 13. Creating a Class • decide what the class should KNOW – these become the attributes (data members) • decide what the class should Do – these become the behaviours (methods / functions / operations) January 233 Katrin Slide 13
  • 14. Account Class Questions to ask: • What does an object of this sort need to remember (what are its attributes)? • • • • current balance [type?] overdraft limit [type?] account name [type?] is account open? [type?] "status" January 233 Katrin Slide 14
  • 15. Account Class Questions to ask: • What does an object of this sort need to do (what are its behaviours)? – access (the "Gets") – open / close this account – print (display) the balance – accept a deposit – process a withdrawal January 233 Katrin Slide 15
  • 17. Open & Close • change the "status" to open: just change status to close: must be empty first January 233 Katrin Slide 17
  • 18. print • do some output January 233 Katrin Slide 18
  • 19. accept a deposit • • • • make sure we're open ask user for it check value of deposit add to balance January 233 Katrin Slide 19
  • 20. process a withdrawal • make sure we're open • ask user for amount • check value of withdrawal – (check for overdraft) • subtract from balance January 233 Katrin Slide 20
  • 21. Do a transfer? • How is this different from a deposit / withdrawal? • We only ask user for one amount... • treat one side as a withdrawal ('cause we already have the code for that) • treat the other side as a deposit but we shouldn't ask the user for the amount, so we need another function January 233 Katrin Slide 21
  • 22. What about constructors? public Account (int ident) { id = ident; } public Account (int ident, int OD) { id = ident; overdraft = OD; } TWO? January 233 Katrin Slide 22
  • 23. What does that leave for the main class? • in main function: // declare an array for 5 accounts Account[] list = new Account[MAX]; // fill the array with the actual accounts for (int i = 0; i < MAX; i++) list[i] = new Account(i); January 233 Katrin Slide 23
  • 24. Figuring out which Account: acctno = chooseAccount(NEW, list); //or acctno = chooseAccount(OLD, list); • this will need it's own little menu January 233 Katrin Slide 24
  • 25. once we have an account: case 'w' : System.out.print ("Doing Withdrawal....n"); amount = list[acctno].doWithdrawal(); break; case 'o' : System.out.print("New Account.n"); list[acctno].openAcct(); break; January 233 Katrin Slide 25
  • 26. Doing a transfer - 1 System.out.print ("Account to transfer FROM:n"); acctfrom = chooseAccount(OLD, list); System.out.print ("Account to transfer INTO:n"); acctto = chooseAccount(OLD, list); January 233 Katrin Slide 26
  • 27. Doing a transfer - 2 amount = list[acctfrom].doWithdrawal(); if (amount!= 0.0) result = list[acctto].doTransfer(amount); if (result != 0.0) System.out.print ("Transfer Successful.nn"); else /* didn't work... put it back */ list[acctfrom].doTransfer(amount); January 233 Katrin Slide 27
  • 28. Putting it together: the Account: public class Account { // the vars // the constructor(s) // the methods } January 233 Katrin Slide 28
  • 29. Putting it together: the Bank: public class Bank { // the static vars // the static methods public static void main(String[] args) { Account list[] = new Account[MAX]; for (int i = 0; i < MAX; i++) list[i] = new Account(i); // do stuff with them }// end main }// end Bank January 233 Katrin Slide 29
  • 30. Bank Class The Classes: chooseAccount doTransfer main Account Class balance limit open? name Account(s) the "Gets" doTransfer openAccount closeAccount showBalance January 233 Katrin doDeposit doWithdrawal Slide 30