OBJECT-ORIENTED PROGRAMMING Flashcards
Everything in OOP is associated with classes and objects, Describe the differences between Class and Object.
CLASS
A class is a template for
objects.
OBJECT
An object is a member or an
“instance” of a class.
CLASS
A class defines object properties including a valid range of values, and a default value.
OBJECT
An object has a state in which all of its properties have values you either explicitly define or that are defined by default settings.
CLASS
A class also describes object
behavior.
State TWO (2) object-oriented programming characteristic and explain the characteristics.
ABSTRACTION
* Process of taking away or removing characteristics from an object in order
to reduce it to a set of essential characteristics.
ENCAPSULATION (DATA HIDING)
* Binding or wrapping‐up of data and associated methods into a single unit.
The goal of the SDLC is to produce superior software that meets and all customer expectations and demands. List FIVE (5) stages of Software Cycle.
- Analysis
- Design
- Coding
- Testing
- Operation and Maintenance
Reserved words in Java serve as a code key. These words can’t be use anything else because they’re predefined. State FIVE (5) java reserved words.
- class
- boolean
- int
- double
- float
* public
* static
* void
* if
* else
```
public class Cinema
{
private String movieHall;
private String movieTitIe;
private double price;
private String modePayment; // QRPayment, Cash private String membership; //
definition of methods
}
~~~
A parameterized/normal constructor to initialize the object values.
public Cinema (String movieHall, String movieTitle, double price, String modePayment, Stringmembership) { movieHall = mH; movieTitle = mT; price = p; modePayment = mP; membership = m; }
```
public class Cinema
{
private String movieHall;
private String movieTitIe;
private double price;
private String modePayment; // QRPayment, Cash private String membership; //
definition of methods
}
~~~
A mutator and accessor methods for all data members.
Accessor (Getter)
public String getMovieHall() { return movieHall; } public String getMovieTitle() { return movieTitle; } public double getPrice() { return price; } public String getModePayment() { return modePayment; } public String getMembership() { return membership; }
Mutator (Setters)
public void setMovieHall(String movieHall) { movieHall = mH; } public void setMovieTitle(String movieTitle) { movieTitle = mT; } public void setPrice(double price) { price = p; } public void setModePayment(String modePayment) { modePayment = mP; } public void setMembership(String membership) { membership = m; }
The method calculateDiscount() to return the discount rate based
on the mode of payment and membership of every customer.
The table at Google Classroom ‘Exercise’ file.
public calculateDiscount() { double price; double discount; if (modePayment.equalsIgnoreCase("QR")) { if (membership.equalsIgnoreCase("True")) { discountRate = price*7/100; } else { discountRate = price*3/100; } } else if (modePayment.equalsIgnoreCase("Cash")) { if (membership.equalsIgnoreCase("True ")) { discountRate = price*10/100; } else { discountRate = price*5/100; } } return discountRate; } }
Write a program to ask user to input and store all information needed.
The table at Google Classroom ‘Exercise’ file.
main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.println("Are you member? "); String membership = input.next(); System.out.println(“Please insert price"); double price = input.nextDouble(); String movieHall = scanner.nextLine(); System.out.println("QR or Cash"); String modePayment = input.next(); }
A printer method to display all the information.
The table at Google Classroom ‘Exercise’ file.
public static void Print() { System.out.println("Movie Hall: " + movieHall); System.out.println("Movie Title: " + movieTitle); System.out.println("Price: " + price); System.out.println("Mode of Payment: " + modePayment); System.out.println("Membership: " + membership); System.out.println("Discount Rate: " + (calculateDiscount() * 100) + "%"); }
Create an array of object hallCapacity that can store hundreds (150) data.
Hall [ ] hallCapacity = new Hall[150];
Java package for input-output program.
import java.io.*;
To open a file name** stock.txt**t for reading, totalstock.txt for writing, and printing process. (Include the class declaration).
import java.io.*; import java.util.*;
public class scoreboard { public static void main(String[] args) throws Exception { try{ BufferedReader fr = new BufferedReader (new FileReader("score.txt")); Bufferedwriter br = new Bufferedwriter (new FileWriter ("scoreout.txt")); Printwriter pr = new PrintWriter (br) ;
To close all file streams in used.
fr.close(); br.close();
To handle exception for file input-output processes.
catch (FileNotFoundException fnf) { System.out.println(fnf.getMessage()); } catch (EOFException eof) { System.out.println(eof.getMessage()); } catch (IOException io) { System.out.println(io.getMessage()); }
Write a program that will calculate the length of the array for element show below:
int [ ][ ] a = { {1, 2, 3}, {4, 5, 6, 9}, {7} }
public class ArrayLengthCalculator { public static void main(String[] args) { int [ ][ ] a = { {1, 2, 3}, {4, 5, 6, 9}, {7} }; int totalLength = 0; for (int i = 0; i < a.length; i++) { totalLength += a[i].length; } System.out.println("Total length of the array: " + totalLength); } }
Give FIVE (5) differences between Structured Programming and OOP.
1
Structure Programming
Focuses on process
OOP
Focuses on data
2
Structure Programming
Using top-down approach
OOP
Using bottom up approach
3
Structure Programming
Programs are divided into small self contained functions
OOP
Programs are divided into small
entities called objects
4
Structure Programming
Less secure as there is no way of data hiding
OOP
More secure since it has data hiding features
5
Structure Programming
Can solve moderately complex
programs
OOP
Can solve any complex programs
State one object and give two (2) examples for
each of the elements.
**Object: **
Car
Attribute:
-White
-Tesla
Behavior:
- Accelerate: The car can accelerate when the gas pedal is pressed.
- Brake: The car can apply brakes to slow down or stop.
State:
- 260 km/h
- Range <600 km
State FOUR (4) object-oriented programming advantages.
- Save time and high productivity
- Secure program codes
- Easy to partition the work based on objects
- Easy to upgrade from smaller to larger system
Write the coding for all question below.
Write a program to input marks of five subjects Physics, Chemistry,
Biology, Mathematics and Computer. Calculate percentage and grade
according to following:
Percentage <= 90% : Grade A
Percentage <= 80% : Grade B
Percentage <= 70% : Grade C
Percentage <= 60% : Grade D
Percentage <= 40% : Grade E
Percentage < 40% : Grade F
public class Main (
public static void main(String[] args) { Scanner in = new Scanner(System.in);
System.out.println(“Enter the marks of five subjects: :\n”);
// To store the values of five subjects
float sub_1 in.nextFloat();
float sub 2 in.nextFloat();
float sub 3 in.nextFloat();
float sub 4 in.nextFloat();
float sub 5 in.nextFloat();
float total;
float average;
float percentage;
char grade;
// Calculate total, average and percentage total = sub_1+ sub_2+ sub_3+ sub_4+ sub_5;
average (float) (total / 5.0);
percentage= (float) ((total / 500.0) * 100);
// It will calculate the Grade
if (percentage <= 90) {
grade = ‘A’;
} else if (percentage <= 80) {
grade = ‘B’;
} else if (percentage <= 70) {
grade = ‘C’;
} else if (percentage <= 60) {
grade = ‘D’;
} else if (percentage <= 40) {
grade = ‘E’;
} else {
grade = ‘F’;
}
System.out.println(“Total marks: “ + total);
System.out.println(“Average: “ + average);
System.out.println(“Percentage: “ + percentage + “%”);
System.out.println(“Grade: “ + grade);
}
}
Identify the error in the following code and explain the error of the
code.
public class Animal { private String name; private int age; public Animal(String name, int age) { name = name; age = age; }
this.name = name; this.age = age;
Use this.name = name;
and this.age = age;
to correctly assign the constructor parameters to the class’s private fields.
Write a program to calculate the sum of 3 numbers.
public class Main( public static void main (String[] args) { int num1=7; int num2=9; int num3=123; int sum=num1+num2+num3; System.out.print("the sum is "); System.out.println(sum); } }
Write a program to calculate BMI. The user will need to input their name, height (in meter), weight (in kg) and age. The program will then print/display all the user information and user BMI.
The BMI formula is as follow:
BMI = weight / (height * height)
import java.util.Scanner; public class Example ( public static void main(String args[]) { Scanner sc =new Scanner(System.in); System.out.print ("Input weight in kilogram: "); double weight = sc.nextDouble(); System.out.print ("Input height in meters: "); double height = sc.nextDouble(); double BMI = weight / (height height); System.out.print("The Body Mass Index (BMI) is + BMI + kg/m2"); } }