0% found this document useful (0 votes)
20 views11 pages

Java Revison For Exam

This document serves as a Java revision guide covering key concepts in object-oriented programming such as objects, classes, data abstraction, encapsulation, inheritance, and polymorphism. It includes practical code examples for printing messages, reading user input, and calculating areas, along with assignment questions and their solutions. Additionally, it explains Java's primitive and non-primitive data types, import statements, and methods for displaying messages in dialog boxes.

Uploaded by

yadakdavid67
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)
20 views11 pages

Java Revison For Exam

This document serves as a Java revision guide covering key concepts in object-oriented programming such as objects, classes, data abstraction, encapsulation, inheritance, and polymorphism. It includes practical code examples for printing messages, reading user input, and calculating areas, along with assignment questions and their solutions. Additionally, it explains Java's primitive and non-primitive data types, import statements, and methods for displaying messages in dialog boxes.

Uploaded by

yadakdavid67
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/ 11

JAVA REVISON FOR EXAM

OBJECTS: Objects are basic run time entities in an object oriented system that
represents anything from a person, a place, bank account, table of data. It can also
represent user-defined data such as lists, vectors and time. Objects contains
data(attributes) and methods(behavior).

OBJECTS: STUDENT

DATA:
Name
Date of birth
Marks

METHODS:
TOTAL
AVERAGE
DISPLAY

CLASSES: A class is a user-defined data type with a collection of objects which


share common characteristics and common properties. It defines the fields and
methods that objects of that class will have.

DATA ABSTRACTION: It refers to the act of representing essential features


without including the background details or explanation

ENCAPSULATION: It is the wrapping up of data and function into a single unit


called a class.

INHERITANCE: It is the process by which objects of one class acquire the


properties of objects of another class.
POLYMORPHISM: It is an important OOP concept. It is a Greek term that means
the ability to take more than one form. It is the ability of objects of different classes
to be treated as objects of a common class.

OBJECT ORIENTED PROGRAMMING (OOP): It is a programming paradigm


centered on the concept of “objects”, which are instances of classes.

METHOD: It is a named block of code that performs a specific task. Methods are
defined within classes and can be called to execute their functionality.

CODE TO PRINT “Welcome to Java”

public class Welcome {

public static void main(String[] args) {

//Display message 'Welcome to Java! to the console

System.out.println("Welcome to Java");

Using this structure above, we will explain

i. Class
ii. Main method
iii. Statement terminator
iv. Reserved words
v. Comment

CLASS: As we know, class contains objects and method. In the code above,
Public class Welcome is the class. The class name is “Welcome”. A class must
always start with an uppercase. The Public there means that the class Welcome
can be accessed by any other classes in the same package or not.

MAIN METHOD: This is the part of the program where execution starts from.
The main is the name of the method, it is fixed and must be spelt exactly as
“main”.

public: Indicates that the method can be accessed from outside the class.

static: Indicates that the method belongs to the class itself rather than to
instances of the class.

String[] args: The parameter of the main method, which is an array of strings
representing any command-line arguments passed to the program.

STAEMENT TERMINATOR: It is a character that marks the end of a


statement. In the code above, the semicolon(;) is used as the statement
terminator, placed at the end of a statement to indicate to the compiler that the
statement has ended.

RESERVED WORDS: These are predefined keywords that have special


meanings and cannot be used as identifiers. They already have specific
functionalities. The reserved words in the code are:

i. Public
ii. Class
iii. Static
iv. Void
v. Main
vi. String

COMMENTS: These are texts within the source code that gets ignored by the
compiler and do not affect the program‟s execution. They are used to add
explanations, notes or reminders to the code. The comment used in the code
above is a single line comment.

// Display message 'Welcome to Java! to the console


Show the ouput of the following code

public class TEST {

public static void main(String[] args) {

System.out.println("3.5 * 4 / 2.5 is ");

System.out.println(3.5 * 4 / 2 -2.5);

The output of the code above is:

3.5 * 4 / 2.5 is

4.5

Short note: System.out.println("3.5 * 4 / 2.5 is "); this line of code came out
as a string because it was placed in double quotations which is used to identify
strings.

DISPLAYING TEXT IN A MESSAGE DIALOGBOX


showMessageDialog is a method in the JOptionPane class.It is used to display
any text in a message dialog box.

A CODE TO DISPLAY “I love programming in Java”, in a message dialog box

import javax.swing.JOptionPane;

public class TEST {

public static void main(String[] args) {

//Display I love programming in Java in a messsage dialog box


JOptionPane.showMessageDialog(null, "I love programming in Java");

There are two types of import statements in Java

1. Specific import
2. Wildcard import
The specific import specifies single class in the import statement. For
example, the following statement import JOptionPane from package
javax.swing
Import javax.swing.JOptionPane;

The wildcard import imports all the classes in a package. For example, the
following imports all classes from the package javax.swing
Import javax.swing.*;

PRIMITIVE JAVA DATA TYPES


i. byte – represents 8-bits integer value
ii. short – 16-bits
iii. int – 32-bits
iv. long – 64-bits
v. float – 32-bit floating value
vi. double – 64-bit floating value
vii. char – 16-bit Unicode character eg „a‟
viii. boolean – represents Boolean value (either „true „or „false‟)
Primitive data types are basic data types built into the programming
language

SOME NON-PRIMITIVE TYPES ARE:

1. string – represents a sequence of characters


2. array
3. classes and objects
Non-primitive are not built into the programming language but are
instead user defined by the programmer or provided by libraries.

SIMPLE CODE TO COMPUTE AREA OF A CIRCLE

public class TEST {

public static void main(String[] args) {

//code to compute the area of a circle

double radius;

double area;

radius = 20;

area = radius * radius * 3.1459;

System.out.println("The area of a circle is " + area);

READING INPUT FROM THE USER

Import java.util.Scanner means to import the scanner class from java.util

Scanner input = new Scanner(System.in) means to create a scanner object which


informs the compiler that a user be asked to give input

Input.nextDouble() reads a number of the data type double.

It goes the same way for other primitive data types.

For String, its “ Input.nextLine()

In this section, we ask user for input of radius to solve for area of
circle
import java.util.Scanner;

public class TEST {

public static void main(String[] args) {

//Create a scanner object

Scanner input = new Scanner(System.in);

//Ask user for input for radius

System.out.println("Enter a number for radius: ");

double radius = input.nextDouble();

double area = 3.1459 * radius * radius;

System.out.println(area);

ASSIGNMENT QUESTIONS:

1. Write a program to calculate the sum and average of a series of „n‟ numbers

ANSWER:

import java.util.Scanner;

public class Test {

public static void main(String[] args) {

Scanner input = new Scanner(System.in);


System.out.print("Enter the value of n: ");
int n = input.nextInt();
int sum = 0;
System.out.print("Enter " + n + " numbers, each in a new line:");

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


int num = input.nextInt();
sum += num;
}

double average = (double) sum / n;


System.out.println("Sum: " + sum);
System.out.println("Average: " + average);
}

QUESTION 2: Write a Java code to compute the average of three numbers, print
out the result and the largest number

ANSWER:

import java.util.Scanner;

public class Test {


public static void main(String[] args) {
Scanner input = new Scanner(System.in);

System.out.print("Enter a number: ");

int firstNumber = input.nextInt();

System.out.print("Enter the second number: ");

int secondNumber = input.nextInt();

System.out.print("Enter the third number: ");

int thirdNumber = input.nextInt();

double average = (firstNumber + secondNumber + thirdNumber) / 3;


System.out.println("The average of the three numbers is: " + average);

if (firstNumber > secondNumber && firstNumber > thirdNumber) {

System.out.println("The largest number is: " + firstNumber);

else if (secondNumber > firstNumber && secondNumber > thirdNumber)

System.out.println("The largest number is: " + secondNumber);

else {

System.out.println("The largest number is: " + thirdNumber);

QUESTION3: write a simple java program to implement a book ordering


promotion policy based on the following conditions:

Books ordered from the store with order size of five(5) and above attracts 5%
discount,
if the order size is less than five(5), 2% discount is given;
books ordered from publisher with order size of thirty(30) and above attracts 20%
discount,
if the order is between twenty(20) and thirty(30), 10% discount is given,
if the order size is between ten(10) and
twenty(20), 5% discount is given,
any order below ten(10) attracts no discount; books ordered from salesman attracts
no discount.

ANSWER:

import java.util.Scanner;
public class TEST {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter order type (store, publisher, salesman): ");
String orderType = scanner.nextLine().toLowerCase();
System.out.println("Enter order size: ");
int orderSize = scanner.nextInt();
double discount = calculateDiscount(orderType, orderSize);
if (discount == 0) {
System.out.println("No discount applicable.");
} else {
System.out.println("Discount applicable: " + discount + "%");
}
scanner.close();
}
public static double calculateDiscount(String orderType, int orderSize) {
double discount = 0;
switch(orderType) {
case "store":
if (orderSize >= 5) {
discount = 5;
} else {
discount = 2;
}
break;
case "publisher":
if (orderSize >= 30) {
discount = 20;
} else if (orderSize >= 20) {
discount = 10;
} else if (orderSize >= 10) {
discount = 5;
}
break;
case "salesman":
// No discount for orders from salesman
break;
default:
System.out.println("Invalid order type.");
}
return discount;
}
}

You might also like