0% found this document useful (0 votes)
6 views6 pages

Untitled Document

The document provides an overview of Object-Oriented Programming (OOP), including definitions of key concepts such as classes, methods, properties, and objects, along with the properties of OOP like encapsulation and inheritance. It also includes Java programming syntax examples, including control statements, escape sequences, and basic program structures, as well as sample programs for calculating BMI, solving quadratic equations, and grading GPAs. Additionally, it covers a mini payroll program and the basics of Java applets, including how to compile and run them.

Uploaded by

marveysedit
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)
6 views6 pages

Untitled Document

The document provides an overview of Object-Oriented Programming (OOP), including definitions of key concepts such as classes, methods, properties, and objects, along with the properties of OOP like encapsulation and inheritance. It also includes Java programming syntax examples, including control statements, escape sequences, and basic program structures, as well as sample programs for calculating BMI, solving quadratic equations, and grading GPAs. Additionally, it covers a mini payroll program and the basics of Java applets, including how to compile and run them.

Uploaded by

marveysedit
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/ 6

QUESTION 1

a) What is Object Oriented Programming?


Object-Oriented Programming (OOP) is a programming paradigm based on the concept of
"objects" which contain data (fields) and code (methods). It emphasizes reusability, modularity,
and abstraction.

b) Explain the following:

Class: A blueprint for creating objects, defining properties (fields) and behaviors (methods).

Method: A block of code inside a class that performs an action or computes a value.

Properties: Also called fields or attributes, these store the state/data of an object.

Object: An instance of a class, representing a specific entity with behavior and state.

c) State the properties of OOP

Encapsulation

Inheritance

Polymorphism

Abstraction

---

QUESTION 2

a) Syntax and examples:

if statement:

if (a > b) {
System.out.println("a is greater");
}

if...else statement:
if (a > b) {
System.out.println("a is greater");
} else {
System.out.println("b is greater");
}

For...Next (In Java, it's just for loop):

for (int i = 1; i <= 10; i++) {


System.out.println(i);
}

b) Use of escape sequence in Java:


Escape sequences like \n (new line), \t (tab), \\ (backslash), \" (double quote) help format
strings.
Example:

System.out.println("Hello\tWorld\nNew Line");

c) Structure of a Java program:

public class Main {


public static void main(String[] args) {
System.out.println("Hello World");
}
}

---

QUESTION 3

a) BMI Calculator Program:

import java.util.Scanner;

public class BMI {


public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter weight in kg: ");
double weight = input.nextDouble();
System.out.print("Enter height in meters: ");
double height = input.nextDouble();
double bmi = weight / (height * height);
System.out.println("Your BMI is: " + bmi);
}
}

b) Statement explanations:

import java.util.Scanner; – Imports the Scanner class to read input.

System.out.println() – Prints output to the console.

public class Main – Defines the main class of the program.

---

QUESTION 4

a) Print numbers from 1-100 using For loop:

public class PrintNumbers {


public static void main(String[] args) {
for (int i = 1; i <= 100; i++) {
System.out.println(i);
}
}
}

b) Quadratic Equation Solver:

import java.util.Scanner;

public class Quadratic {


public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter a, b, c: ");
double a = input.nextDouble();
double b = input.nextDouble();
double c = input.nextDouble();

double discriminant = b * b - 4 * a * c;
double root1 = (-b + Math.sqrt(discriminant)) / (2 * a);
double root2 = (-b - Math.sqrt(discriminant)) / (2 * a);

System.out.println("Roots: " + root1 + " and " + root2);


}
}

---

QUESTION 5

a) GPA Grading Program:

import java.util.Scanner;

public class GPAProgram {


public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter GPA: ");
double gpa = input.nextDouble();

if (gpa >= 3.5 && gpa <= 4.0) {


System.out.println("Distinction");
} else if (gpa >= 3.0) {
System.out.println("Upper Credit");
} else if (gpa >= 2.5) {
System.out.println("Lower Credit");
} else if (gpa >= 2.0) {
System.out.println("Pass");
} else {
System.out.println("Fail");
}
}
}

b) Ways to comment in Java:

Single-line: // This is a comment

Multi-line:

/* This is a
multi-line comment */

c) Use of ; in Java:
The semicolon ; marks the end of a statement in Java.

---

QUESTION 6

Mini Payroll Program:

import java.util.Scanner;

public class Payroll {


public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter hours worked: ");
double hours = input.nextDouble();
System.out.print("Enter rate per hour: ");
double rate = input.nextDouble();

double wage = hours * rate;


double tax;
if (wage > 10000) {
tax = 0.15 * wage;
} else {
tax = 0.015 * wage;
}
double netpay = wage - tax;
System.out.println("Net Pay: " + netpay);
}
}

---

QUESTION 7

a) Java Applet:
A Java applet is a small program that runs in a web browser and is embedded in an HTML
page.

b) Running Java Applet:


1. Compile the applet: javac MyApplet.java

2. Create an HTML file that embeds the applet.

3. Use appletviewer MyApplet.html to run.

You might also like