college/Summer-2024/CS-3443/Slides/txt/02_Java-Basics.txt

305 lines
4.0 KiB
Plaintext
Raw Permalink Normal View History

2024-08-31 02:13:09 -05:00
Application
Programming
Hend Alkittawi
Java Basics
Introduction to Java Concepts and
User-defined Java Classes
CONCEPTS
JAVA
OBJECT
- Released in early 1990s
- Defined by a class
- Object-oriented
- A class is a reusable
- Java SE, Java EE, Java ME
software components
- Non-primitive data type
CONCEPTS
JAVA APIs
- Application Programming
Interfaces
- Collections of existing
classes and methods in the
Java class libraries
JDK
- Java Development Kit
- Includes compiler, loader,
debugger, JRE, and other
tools for Java development
CONCEPTS
JVM
- Java Virtual Machine
- A part of the JRE that
IDE
- Integrated Development
Environment
executes bytecodes.
- A software that provides
- Bytecodes are platform
comprehensive facilities
independent (portable)
for software development.
WHY DO WE USE OBJECTS?
Let us discuss the difference between
Write a program to read n number of values in an array and
display them in reverse order.
Write a program to calculate x raised to the power n (xn).
Write a program to swap two numbers.
and
Small Scale Applications
OBJECT ANALOGY
BLUEPRINTS
My Car
color, …
accelerate, …
Your Car
Some Car
color, …
accelerate, …
color, …
accelerate, …
JAVA OBJECTS
public class Car {
Class Declaration
private int colorID;
private double speed;
public void setSpeed(int s){
speed = s;
}
public void accelerate(){
speed = speed + 70;
}
Attributes / Instance Variable(s)
Behaviors / Method(s)
public static void main(String[] args){
Car myCar = new Car();
myCar.setSpeed(50);
}
}
Car.java
Instantiation (building a Car object)
OBJECT ANALOGY
BIOLOGICAL
CLASSIFICATION
My Dog
breed, …
bark, …
Your Dog
breed, …
bark, …
Some Dog
breed, …
bark, …
JAVA OBJECTS
public class Dog {
Class Declaration
private int breedID;
Attributes / Instance Variable(s)
public void bark(){
System.out.println(Woof);
}
Behaviors / Method(s)
Dog.java
public static void main(String[] args){
Dog myDog = new Dog();
Instantiation (building a Dog obj)
myDog.bark();
}
}
THE String CLASS
-
The char primitive type stores one character of the Unicode
character set.
-
-
A string is a sequences of characters.
-
can be 0 or more characters long
-
"" is the empty string
The String class facilitates handling multiple characters at
once.
-
A String object can be declared in order to work with strings
String someStringObject = new String();
someStringObject = "HI";
THE String CLASS
-
Many String manipulation methods are available, here are some:
-
split() method to get an array of Strings from a String, based
on a delimiter. This object method takes a delimiter as a
parameter.
-
trim() method to clear off any additional space from the text.
This object method takes no parameters.
-
charAt() method to get the character at an index in the string
[starting with index 0]. This object method takes an index as
a parameter.
-
equals() method to check if two Strings contain the same text.
This object method takes a String object as a parameter.
THE String CLASS
-
Java also treats string literals as objects
-
-
A string literal is a quoted string: "Sam I am", "Hi", …, etc.
Examples:
String strObj = "This is \"a\" String";
strObj.charAt(0);
"HI
".trim();
escape sequence
"Hello".equals(strObj);
Note how we can create/work with string literals as String
objects.
JAVA OBJECTS
public class Dog {
private int breedID;
private String name;
public void bark(String dogBark){
System.out.println(dogBark);
}
}
public static void main(String[] args){
Dog myDog = new Dog();
myDog.bark(Woof);
}
Dog.java
Class Activity
public class SimpleLocation {
private double latitude;
private double longitude;
public void setLocation(double lat, double lon){
latitude = lat;
longitude = lon;
}
}
public static void main(String[] args){
SimpleLocation utsa = new SimpleLocation();
utsa.setLocation(32.9, -117.2);
}
What is
the value
of
longitude
?
DO YOU HAVE ANY
QUESTIONS?
THANK
YOU!
@
hend.alkittawi@utsa.edu
By Appointment
Online