Java Revison For Exam
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
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.
System.out.println("Welcome to Java");
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.
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.
System.out.println(3.5 * 4 / 2 -2.5);
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.
import javax.swing.JOptionPane;
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.*;
double radius;
double area;
radius = 20;
In this section, we ask user for input of radius to solve for area of
circle
import java.util.Scanner;
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;
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;
else {
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;
}
}