Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
1
Software design Activities
Communication
Planning
Modeling
◦ Analysis of requirements
◦ Design
Construction
◦ Code generation
◦ Testing
Deployment
Class and object
To represent data we need class, and class is the core
concept of OOPS. Class is a blueprint of object that contains
variables and methods. It is logical representation of data.
Object − Objects have states and behaviors. Example: A dog
has states - color, name, breed as well as behaviors – wagging
the tail, barking, eating.An object is an instance of a class.
Let us now look deep into what are objects. If we consider
the real-world, we can find many objects around us, cars,
dogs, humans, etc. All these objects have a state and a
behavior.
If we consider a dog, then its state is - name, breed, color, and
the behavior is - barking, wagging the tail, running.
3
General form of a class
class Vehicle{
class classname{
// declare instance variable
int passenger; // number of passenger
Type var1; int fulecap; // fuel capacity
Type var 2; int mpg; // fuel consumption in miles
per gallon
// declare method }
Type method1(parameter)
{
Body of the method
}
Type method 2(parameter)
{
Body of the method
}
4
local variable Variables
Local variables are declared in methods, constructors, or blocks.
Local variables are visible only within the declared method, constructor,
or block.
Instance variables are created when an object is created with the use of
the keyword 'new' and destroyed when the object is destroyed.
Instance variables have default values. For numbers, the default value is
0, for Booleans it is false. Values can be assigned during the declaration or
within the constructor.
Instance Variable
import java.io.*; }
public class Employee { // This method prints the employee
// this instance variable is visible for details.
any child class. public void printEmp() {
public String name; System.out.println("name : " + name
// salary variable is visible in );
Employee class only. System.out.println("salary :" +
private double salary; salary);
// The name variable is assigned in }
the constructor. public static void main(String args[])
public Employee (String empName) { {
name = empName; Employee empOne = new
} Employee("Ransika");
// The salary variable is assigned a empOne.setSalary(1000);
value. empOne.printEmp();
public void setSalary(double empSal) }
{ }
salary = empSal;
Instance Variable: program Example
public class Add
{
float b;
int a;
boolean c;
public static void main(String args[])
{
Add p = new Add();
System.out.println(p.b);
System.out.println(p.a);
System.out.println(p.c);
}
}
Access Modifier: public
class Sample{ q.x=10;
int x; q.y= (float) 10.5;
float y;
} System.out.println(q.x);
public class Add1 System.out.println(q.y);
{
float b; }
int a; }
boolean c;
public static void main(String args[])
{
Add1 p = new Add1();
System.out.println(p.b);
System.out.println(p.a);
System.out.println(p.c);
Sample q=new Sample();
Access Modifier: private
class Sample{ q.x=10;
private int x; q.y= (float) 10.5;
private float y;
} System.out.println(q.x);
public class Add1 System.out.println(q.y);
{
float b; }
int a; }
boolean c;
public static void main(String args[])
{
Add1 p = new Add1();
System.out.println(p.b);
System.out.println(p.a);
System.out.println(p.c);
Sample q=new Sample();
Class: Program concept
class Vehicle{
After creating object, it will hold all the instance variable defined in class.
Syntax: object.member;
Object creation: Alternate approach
Vehicle minivan; // declares references to object
minivan=new Vehicle();
Vehicle car2=car1;
Class: Program example
class Vehicle{
int passenger; // number of passenger
int fulecap; // fuel capacity
int mpg; // fuel consumption in miles per gallon
}
Class vehicledemo{
public static void main(String args[]){
Vehicle minivan=new Vehicle();
int range;
minivan.passenger=7;
minivan.fuelcap=16;
minivan.mpg=21;
range=minivan.fuelcap*minivan.mpg;
System.out.println(“minivan can caryy” + minivan.passenger + “with a
range of “ + range);
}
}
Adding method to a class
Parameterized method to a class
User input variable: Program example
import java.util.Scanner;
class vehicle{
int passenger;
}