Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
35 views

Lecture04 Annotated

This document discusses the String class in Java and various methods that can be used on String objects like charAt(), concat(), length(), substring(), replace(), equals(), compareTo(), and toLowerCase()/toUpperCase(). It also discusses comparing different variable types like floats, chars, Strings, and objects using ==, equals(), compareTo(), and checking for equality of all fields. The document explains parameter passing in methods and how primitive types are passed by value while objects are passed by reference. It provides an example Employee class to demonstrate linking objects using references.

Uploaded by

Lewi Fasil
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
35 views

Lecture04 Annotated

This document discusses the String class in Java and various methods that can be used on String objects like charAt(), concat(), length(), substring(), replace(), equals(), compareTo(), and toLowerCase()/toUpperCase(). It also discusses comparing different variable types like floats, chars, Strings, and objects using ==, equals(), compareTo(), and checking for equality of all fields. The document explains parameter passing in methods and how primitive types are passed by value while objects are passed by reference. It provides an example Employee class to demonstrate linking objects using references.

Uploaded by

Lewi Fasil
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 25

COE 318

Introduction to Software Design


Lecture 04

Dr. Naimul Khan


Office: Virtual
Office Hour: By appointment
The String class
• One of the many examples of Java built-in
classes.

String String licensePlate;


object licensePlate= new String ("ABC 41");

String licensePlate="ABC 41";


String String licensePlate2="ABC 41";
//A special case only for Strings
literal //new memory is not allocated if same string, existing
//string is used
String licensePlate;

licensePlate= new String ("ABC 41");

char a= licensePlate.charAt(2); //index starts at zero

String extendedPlate= licensePlate.concat("564");

int b= licensePlate.length();

String newPlate= extendedPlate.replace('4', '0');


String licensePlate;

licensePlate= new String ("ABC 41");

String subPlate= licensePlate.substring(1);

String subPlate2= licensePlate.substring(1,2);

String subPlate3= licensePlate.substring(1,8);

//also check out toLowerCase() and toUpperCase()


Comparing variables - floats
• Floats need to be matched EXACTLY- not
possible sometimes due to approximations
on the hardware
• Solution: Define a variable TOLERANCE
with a small value (e.g. 0.00000001). then:

if(Math.abs(a-b)<TOLERANCE){
}
Comparing variables - chars
• 16 bit unicode for all characters
• 38885 characters currently are defined with
Unicode values.
• The low 8 bits can be used for ASCII.

if('0'<'L'){
}
Comparing variables - Strings
• Remember, String variables are references.
• References CAN be compared with the
"==" operator, but the result won't be what
you expect. Why?
String name1 = new String("Lewis");
String name2 = new String("Lewis");
if(name1==name2){
//condition will be evaluated to false

String name1 = new String("Lewis");


String name2 = new String("Lewis");
name1=name2;
if(name1==name2){
//condition will be evaluated to true

String name1 = "Lewis";


String name2 = "Lewis";
if(name1==name2){
//condition will be evaluated to true
//String literals use String pool, no new
//memory allocated if the strings are the same
}
String name1 = new String("Lewis");
String name2 = new String("Lewis");
if(name1.equals(name2)){ The equals()
//condition will be evaluated to true method
}

Also check the compareTo() method


Comparing objects
public class Car {
String licensePlate;
double speed;
double maxSpeed;
public Car(String plateNumber, double speedVal, double maxVal){
licensePlate=plateNumber;
speed=speedVal;
maxSpeed=maxVal;
}
}
Comparing objects with ==

public class CarTest {


public static void main(String[] args){
Car miniVan = new Car("ABC", 50, 100);
Car sedan=new Car("ABC", 50, 100);
if(sedan==miniVan){
//condition will be evaluated to false
}
}
}
Comparing objects with equals()

public class CarTest {


public static void main(String[] args){
Car miniVan = new Car("ABC", 50, 100);
Car sedan=new Car("ABC", 50, 100);
if(sedan.equals(miniVan)){
//condition will STILL be evaluated to false
}
}
}
What we really want to do is..

public class CarTest {


public static void main(String[] args){
Car miniVan = new Car("ABC", 50, 100);
Car sedan=new Car("ABC", 50, 100);
if(sedan.licensePlate.equals(miniVan.licensePlate) &&
sedan.speed==miniVan.speed && sedan.maxSpeed==miniVan.maxSpeed){
//condition will be evaluated to true
}
}
}
Parameter passing in
Java Methods
• If the parameter is of primitive data type,
parameter is passed by value – changing the
parameter value inside the method has no
effect on the original value
Parameter passing by value

public static void main(String [] args){


double d = 2.0;
changeMe(d);
System.out.println(d);
}
public void changeMe(double d)
{
//this has no effect on d outside of this method!
d = 345.0;
}
Parameter passing in
Java Methods
• However, If the parameter is an object,
parameter is passed by reference – changing
the parameter value inside the method
changes the original value
Parameter passing by reference

public static void main(String[] args){


Car miniVan = new Car("ABC", 50, 100);
changeParameters(miniVan);
System.out.println(miniVan.speed);

}
public void changeParameters(Car c)
{
//changes the car’s speed outside this method!
c.speed=30;
}
Linking objects (Lab 3)
• A variable can hold a reference to another
object. When one object contains a variable
that refers to another object, we think of the
objects as being "linked" by the reference.
• Any variable that can contain a reference to
an object can also contain the special value
null, which refers to nowhere.
Linking objects (Lab 3)
• Example: A class designed to represent
employees at a company. Suppose that
every employee except the CEO has a
supervisor. CEO is another employee of the
company. Then the Employee class would
naturally contain an instance variable of
type Employee that points to the employee's
supervisor:
The Employee Class
class Employee {
// An object of type Employee holds data about one employee.
String name; // Name of the employee.
Employee supervisor; // The employee's supervisor.
Employee (String name, Employee supervisor)
{ this.name = name;
this.supervisor = supervisor;
}
// other methods
}
The Employee Class
class EmployeeTest{
public static void main(String[] args){
Employee emp, emp1;
emp1 = new Employee("Stacey", null);
emp = new Employee("Gavin", emp1); .
if ( emp.supervisor == null) {
System.out.println(emp.name " is the CEO!");}
else {//Since we are checking emp, this line will be printed

System.out.println( "The supervisor of " + emp.name + " is


" + emp.supervisor.name );
}
Practice problems - find output
public static void main(String[] args) {
System.out.println(1 + 6 + 8);
System.out.println("dd" + ((1 > 5) ? "c" : "b"));
System.out.println("a" + (6 + 1) + 3);
System.out.println(7 + "" + (2 > 3));
}
Complete the Airport class based on output
public class Airport {
// Class fields (instance variables) and methods

public static void main(String[] args) {


Airport quizairport;
quizairport = new Airport(5); // creating object with constructor Airport(no. of gates)
for (int i=0; i<5; i++)
quizairport.addPassengers(i,(20*i+3)); // addPassengers
//(gate no., no. of passengers at the gate) to add no. of passengers to a gate
quizairport.printPassengers(); // print gate number and no. of
// passengers at the gate
}}

The output of the program is:


Number of passengers at Gate 1 is 3
Number of passengers at Gate 2 is 23
Number of passengers at Gate 3 is 43
Number of passengers at Gate 4 is 63
Number of passengers at Gate 5 is 83
class Coord { int x, y; Find output
public Coord(int x, int y) { this.x = x;
this.y = y;
}}
class Quiz {
public static void main(String args[])
{ Coord original[], alias[];
original = new Coord[6];
int i = 0;
while (i < original.length) {
original[i] = new Coord(i, original.length - i);
i++; }
i = 0;
while (i < original.length) {
original[i] = original[i + 1];
i += 2; }
alias = original; alias[3].y = alias[0].x;
System.out.println(original[0].x + "," +
original[0].y); System.out.println(alias[3].x + "," +
alias[3].y);
}}

You might also like