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

Core - Defining Classes and Creating Objects

This document introduces classes and objects in Java. It explains that a class defines the type of an object, with member variables to store data and methods to define functionality. An object is an instance of a class that is created using the new keyword. The document demonstrates a SimpleLocation class with latitude and longitude variables and a constructor. It then shows creating SimpleLocation objects, assigning them values, and calling the distance method to print the distance between locations.

Uploaded by

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

Core - Defining Classes and Creating Objects

This document introduces classes and objects in Java. It explains that a class defines the type of an object, with member variables to store data and methods to define functionality. An object is an instance of a class that is created using the new keyword. The document demonstrates a SimpleLocation class with latitude and longitude variables and a constructor. It then shows creating SimpleLocation objects, assigning them values, and calling the distance method to print the distance between locations.

Uploaded by

Radu Iordache
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 22

Classes and Objects in

Java
By the end of this video you will be able to…
▪Motivate the use of classes and objects in programming
▪Write classes in Java
▪Create objects and call methods on them
▪Describe what member variables, methods and
constructors are
Computer Science is…
The science of using and processing
large amounts of information
to automate useful tasks
and learn about the world around us
(using a computer)
Map
Shape
Location
Color
Window
… and plenty more objects
A class is a type of data

An object is one such piece of data*


* With associated functionality
Concept of location
Latitude: 32.9
Longitude: -117.2
Defining a Class
public class SimpleLocation
{ Must be in file
public double latitude; SimpleLocation.java
public double longitude;

public SimpleLocation(double lat, double lon)


{
this.latitude = lat;
this.longitude = lon;
}
public double distance(SimpleLocation other) {
...
public class SimpleLocation
{
public double latitude; Member variables:
public double longitude; data the objects need to store
public SimpleLocation(double lat, double lon)
{
this.latitude = lat;
this.longitude = lon;
}
public double distance(SimpleLocation other) {
...
public class SimpleLocation
{
Methods:
public double latitude;
The things this class can do
public double longitude;

public SimpleLocation(double lat, double lon)


{
this.latitude = lat;
this.longitude = lon;
}
public double distance(SimpleLocation other) {
...
public class SimpleLocation
{
Constructor:
public double latitude;
Method to create a new object
public double longitude;

public SimpleLocation(double lat, double lon)


{
this.latitude = lat;
this.longitude = lon;
}
public double distance(SimpleLocation other) {
...
public class SimpleLocation
{ In file
public double latitude; SimpleLocation.java
public double longitude;

public SimpleLocation(double lat, double lon)


{
this.latitude = lat;
this.longitude = lon;
}
public double distance(SimpleLocation other)
{
// Body not shown
}
}
Creating and using objects
public class LocationTester In file
{ LocationTester.java
public static void main(String[] args)
{
SimpleLocation ucsd =
new SimpleLocation(32.9, -117.2);
SimpleLocation lima =
new SimpleLocation(-12.0, -77.0);

System.out.println(ucsd.distance(lima));
}
}
public class LocationTester
{
public static void main(String[] args)
{
SimpleLocation ucsd =
new SimpleLocation(32.9, -117.2);
SimpleLocation lima =
new SimpleLocation(-12.0, -77.0);

System.out.println(ucsd.distance(lima));
}
}
public class SimpleLocation
{
public double latitude;
public double longitude;

public SimpleLocation(double lat, double lon)


{
this.latitude = lat;
this.longitude = lon;
}
public double distance(SimpleLocation other) {
...
public class LocationTester
{
public static void main(String[] args)
{
SimpleLocation ucsd =
new SimpleLocation(32.9, -117.2);
SimpleLocation lima =
new SimpleLocation(-12.0, -77.0);

System.out.println(ucsd.distance(lima));
}
}
public class SimpleLocation
{
public double latitude;
public double longitude;

public SimpleLocation(double lat, double lon)


{
this.latitude = lat;
this.longitude = lon;
}
public double distance(SimpleLocation other) {
...
ucsd.distance(lima)

public double distance(SimpleLocation other)


{
return getDist(this.latitude, this.longitude,
other.latitude, other.longitude);
}

"this" is the calling object


public class LocationTester
{
public static void main(String[] args)
{
SimpleLocation ucsd =
new SimpleLocation(32.9, -117.2);
SimpleLocation lima =
new SimpleLocation(-12.0, -77.0);

System.out.println(ucsd.distance(lima));
}
}
% javac *.java
% java LocationTester
6567.659

You might also like