[Go to site: main page, start]

0% found this document useful (0 votes)
7 views31 pages

Java Software Engineering Concepts

This lesson covers key concepts in Java programming, including static fields and methods, access control (public vs. private), class scope, and the use of collections such as ArrayLists, Sets, and Maps. It explains how to manage data within classes, the importance of access control for protecting information, and how to utilize various data structures for storing and manipulating collections of objects. Additionally, it provides code examples to illustrate these concepts in practice.

Uploaded by

19mrasul
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views31 pages

Java Software Engineering Concepts

This lesson covers key concepts in Java programming, including static fields and methods, access control (public vs. private), class scope, and the use of collections such as ArrayLists, Sets, and Maps. It explains how to manage data within classes, the importance of access control for protecting information, and how to utilize various data structures for storing and manipulating collections of objects. Additionally, it provides code examples to illustrate these concepts in practice.

Uploaded by

19mrasul
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd

LESSON 5

Access control, Class


scope, Packages, Java
API Software Engineering

2022 – 2023 a.y.


static types and
methods

● Applies to fields and methods


● Means the field/method
 is defined for the class declaration,
 is not unique for each instance
static
public class Baby {
static int numBabiesMade = 0;
}
[Link] = 100;
Baby b1 = new Baby();
Baby b2 = new Baby();
[Link] = 2;

What is
[Link]?
[Link]?
static example

● Keep track of the number of babies that


have been made.

public class Baby {


int numBabiesMade = 0;
Baby ( ) {
numBabiesMade += 1;
}
}
static field

● Keep track of the number of babies that


have been made.

public class Baby {


static int numBabiesMade = 0;
Baby ( ) {
numBabiesMade += 1;
}
}
static method
public class Baby {
static void cry (Baby theBaby) {
[Link]([Link] + “ cries”);
}
}
Or
public class Baby {
void cry () {
[Link](name + “ cries”);
}
}
static notes

● Non-static method can reference static methods, but not


the other way around
– Why?

public class Baby {


String name = “DMX”;
static void writeName ( ) {
[Link](name);
}
}
main

● Why is main static?

public static void main (String [ ] args) {

}
Access
control
Public vs. Private

● Public: others can use this


● Private: only the class can use this

Public/private applies to
any field or method
Access Control
public class CreditCard {
String cardNumber;
double expenses;
void charge(double amount) {
expenses = expenses + amount;
}
String getCardNumber (String password) {
if ([Link](“SECRET!3*!”)) {
return cardNumber;
}
return “jerkface”;
}
}
Access Control DONE
RIGHT
public class CreditCard {
private String cardNumber;
private double expenses;
public void charge(double amount) {
expenses = expenses + amount;
}
public String getCardNumber (String password) {
if ([Link](“SECRET!3*!”)) {
return cardNumber;
}
return “jerkface”;
}
}
Why Access
Control

● Protect private information (sort of)


● Clarify how others should use your class
● Keep implementation separate from interface
Class scope
Scope
public class Main {
public static void main(String[ ] args) {

// Code here CANNOT use x ● In Java, variables


are only accessible
int x = 100; inside the region
they are created.
This is called
// Code here can use x
scope.
[Link](x);
}
}
Variable updates

public class Baby {


int servings;
void feed(int servings) { ● Which ”servings” is
servings = servings + updated?
servings;
}
void poop ( ) {
[Link](“All
better!”);
servings = 0;
}
Variable updates

public class Baby {


int servings;
void feed(int servings) { ● Which ”servings” is
servings = servings + updated?
servings;
}
void poop ( ) {
● Only Method-level
“servings is
[Link](“All
better!”);
updated!
servings = 0;
}
“this” keyword
● Clarifies scope
● Means “my object”

Usage:
class Example {
int memberVariable;
void setVariable (int newVal ) {
[Link] +=
newVal;
}
}
Variable updates

public class Baby {


int servings;
void feed(int servings) { ● Only Method-level
servings = servings + “servings is
servings; updated!
}
void poop ( ) {
[Link](“All
better!”);
servings = 0;
}
Variable updates

public class Baby {


int servings;
void feed(int servings) { ● Object-level
[Link] = “servings is
[Link] + updated!
servings;
}
void poop ( ) {
[Link](“All
better!”);
servings = 0;
ArrayList
Arrays with items

Create the array bigger than you need


Track the next “available” slot

Book[ ] books = new Book[10];


int nextIndex = 0;

Books[nextIndex] = b;
nextIndex = nextIndex + 1;
Arrays with items

Create the array bigger than you need


Track the next “available” slot

Book[ ] books = new Book[10];


int nextIndex = 0;

Books[nextIndex] = b;
nextIndex = nextIndex + 1;

What if the library expands?


ArrayList

Modifiable list
Internally implemented with arrays

Features
• Get/put items by index
• Add items
• Delete items
• Loop over all items
Array  ArrayList

Book[ ] books = new Book[10]; ArrayList<Book> books


int nextIndex = 0; = new
ArrayList<Book>( );
Books[nextIndex] = b;
nextIndex += 1; [Link](b);
import [Link];
class ArrayListExample {
ArrayLis
public static void main (String[ ] args) { t
ArrayList<String> names = new ArrayList<String>( );
[Link](“Jakhongir”);
[Link](“Eshmat”);
[Link](“Toshmat”);
[Link]([Link]( ));
[Link]([Link](0));
[Link]([Link](1));
[Link](0, “Eshmat”);
[Link](1);
for (int i = 0; i < [Link]( ); i++) {
[Link]([Link](i));
}
for (String s: names) {
[Link](s);
}
}
}
Sets
Like an ArrayList, but
• Only one copy of each object, and
• No array index

Features
• Add objects to the set
• Remove objects from the set
• Is an object in the set?

TreeSet: Sorted (lowest to hightest)


HashSet: Unordered (pseudo-random)
Set
import [Link];
class SetExample {
public static void main (String[ ] args) {
TreeSet<String> strings = new TreeSet<String>( );
[Link](“Jakhongir”);
[Link](“Eshmat”);
[Link](“Toshmat”);
[Link]([Link]( ));
[Link]([Link]());
[Link]([Link]());
[Link](“Eshmat”);
for (String s: strings) {
[Link](s);
}
}
}
Maps

Stores a (key, value) pair of objects


Look up the key, get back the value

Example: Address Book


• Map from names to email addresses

TreeMap: Sorted (lowest to hightest)


HashMap: Unordered (pseudo-random)
import [Link];
class SetExample { Map
public static void main (String[ ] args) {
HashMap<String, String> strings = new HashMap <String, s
String>( );
[Link](“Jakhongir”, “email1@[Link]”);
[Link](“Eshmat”, “email2@[Link]”);
[Link](“Toshmat”, “email3@[Link]”);
[Link]([Link]( ));
[Link](“Eshmat”);
[Link]([Link](“Toshmat”));
for (String s: [Link]( )) {
[Link](s);
}
for (String s: [Link]( )) {
[Link](s);
}
for ([Link]<String, String> pairs : [Link]( )) {
[Link](pairs);
}
}
}
THANK
YOU!
ANY QUESTIONS?

[Link]@[Link]

Abdurasul Bobonazarov

You might also like