0% found this document useful (0 votes)
61 views

EXC - Class, Array of Class

1) The document discusses object-oriented programming concepts in Java like classes, objects, and arrays. A class defines common attributes and behaviors for objects, while objects are instances of classes. 2) The new operator is used to create object instances, and constructors initialize objects. Getter and setter methods allow accessing and modifying attribute values. 3) Arrays can store multiple objects of the same class type, like an array to represent a collection of vehicles in a drive park with different car objects.

Uploaded by

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

EXC - Class, Array of Class

1) The document discusses object-oriented programming concepts in Java like classes, objects, and arrays. A class defines common attributes and behaviors for objects, while objects are instances of classes. 2) The new operator is used to create object instances, and constructors initialize objects. Getter and setter methods allow accessing and modifying attribute values. 3) Arrays can store multiple objects of the same class type, like an array to represent a collection of vehicles in a drive park with different car objects.

Uploaded by

Stojan Kitanov
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

Programming Languages

Classes,
Arrays of Classes
Exercise
Classes and Objects
• Јаva is an ОО language
• Java perceives the world as a collection of mutually
interacting objects
• A class is a general definition of characteristics (attributes)
and actions (methods) which are common for certain objects
from the “real world”
• The objects are instances (individuals) of classes

A Car class
Object Citroen_C5 of the class Car
The new operator
The operator for creating a class instance

Basic formula for object creation


1. Name-of-class name-of-object = new Name-of-class( )

2. Name-of-class name-of-object = new Name-of-class( parameters)

Constructors
Ex.

Car my_car = new Car();

Car my_car= new Car (“Citroen C5”,5, ”Gasoline”, ”Silver”);


3
A bit more about classes
Every class has its own methods and attributes.
Access to them (if allowed) goes as follows
- Name_of_object.method()
- Name_of_object.name_of_attribute
- Name_of_object.method(parameters)

Example:
Car my_car= new Car (“Citroen C5”,5, ”Gasoline”, ”Metallic grey”);

System.out.println(my_car.getName());

What is
getName()?

4
Compiling a JAVA file with
more than one class
1. There must be ONLY ONE public class in one file
2. JAVA file has same name as the public class.
3. public static void main (…) is part of the public class body.
4. Compiling is done with the name of the file i.e. name of the public class
Ex. javac main_class.java
5. After compiling, javac creates .class files for every defined class.

6. Interpreting is done by the public class i.e. java public_class_name


5
Ex. java main_class
Ex. for a custom created class
myclass.java

Ex. Constructor without parameters


class myNumber{
private int num;
• In the main program
public int num2;
public myNumber(){ public class myclass {
num=0; public static void main(String[]ar){
num2=100; myNumber A=new myNumber();
} System.out.println(A.getNum());
int getNum(){ A.setNum(10);
System.out.println(A.getNum());
return num;
System.out.println(A.num2);
}
}
void setNum(int i){ }
num=i;
num2=10*i;
Defined method with which the value of
} attribute broj is obtained
}
Defined method with a parameter with which 6
value is added to attribute broj2
Exercise 1
- Create an INT method using which the value of
attribute num2 will be obtained.

- Create a VOID method called description() which will


output the values of both attributes.

- Create a FLOAT method which will return the average


of the values of both attributes.

- Create a constructor with parameters of myNumber


class, which will initialize both parameters of the
class to values set during constructor invocation..
Ex. myNumber mb= new myNumber(13,10);

Show how methods work.


Ex. Creating a class Car
Vehicle.java
class Car{
private String type;
private int no_seats;
private String fuel; What is the meaning of PRIVATE ?
private String body_color;
Car(){
Constructors without
type="";
and with parameters
no_seats=0;
fuel="";
body_color="";
}
Car (String type, int seats, String fuel, String body_color){
this.type=type;
this.no_seats=seats; this – operator which shows
this.fuel=fuel; that the attribute is a part
this.body_color=body_color;
of the class
}
public string getType(){
return type; Defined method of the class with which the
} name of the instantiated car is obtained
public void setType(String type){
this.type=type; Defined method of the class with which you
} can set up new car type of the already
} instantiated car
Exercise 2
- Enrich the class Car with get and set methods for each of the
attributes.
- Create a main class Vehicle and in it create 2 objects of the Car class:
one using the parameterized constructor (ex. Your favorite car),
another with the constructor without parameters.
- Invoke the get methods for both objects
- Assign values to the attributes of the second object using the
corresponding set methods
- Invoke the get methods of the second object
- Create void method spec() which will return all the data for a specific
car object
- Compare the number of passengers (seats) for both automobiles and
output an appropriate message i.e. which car has more pass. seats.
Arrays of objects

Imagine having N
vehicles, i.e. your
own drive park J.
Arrays of objects of the SAME
class
ArrayVehicles.java
int no_cars=5;
//creating an array of 5 objects of the Car class
Car[] DrivePark= new Car[no_cars];
//initialization of the array elements
DrivePark[0]=new Car("honda crv5", 7, "Diesel", "Cream");
DrivePark[1]=new Car();
DrivePark[2]=new Car("Audi A1",5,"gasoline","white");
DrivePark[3]=new Car();
DrivePark[4]=new Car();
//method invocation for an appropriate array element
DrivePark[3].setFuel("Diesel");
System.out.println(DrivePark[0].getType());
DrivePark[2].spec();

You might also like