P a g e | 16
Program to demonstrate a class.
Program Code:
//[Link]
public class Laptop {
int monitorSize;
String colour;
String processor;
int ram;
int hardDisk;
void turnOn() {
[Link]("Laptop is now turned on");
void turnOff() {
[Link]("Laptop is now turned off");
void idle() {
[Link]("Laptop is now idle");
void running() {
[Link]("Laptop is now running");
AMISH SONI 2200910100019
P a g e | 17
//[Link]
public class LaptopTest {
public static void main(String args[]) {
Laptop ibm = new Laptop();
[Link] = 14;
[Link] = "Black";
[Link] = "core 2";
[Link] = 32;
[Link] = 500;
[Link]();
[Link]();
[Link]();
Input/Output:
Laptop is now turned on
Laptop is now running
Laptop is now turned off
AMISH SONI 2200910100019
P a g e | 18
Program to demonstrate default constructor of a class.
Program Code:
class HelloConstructor {
public HelloConstructor() {
[Link]("Hello Java");
public class HelloConstructorTest {
public static void main(String args[]) {
HelloConstructor h = new HelloConstructor();
Input/Output:
Hello Java
AMISH SONI 2200910100019
P a g e | 19
Program to demonstrate parameterized constructor of a class.
Program Code:
class HelloConstructor2 {
public HelloConstructor2 (String name) {
[Link]("Hello " + name);
public class HelloConstructor2Test {
public static void main(String[] args) {
HelloConstructor2 h = new HelloConstructor2("Rex");
Input/Output:
Hello Rex
AMISH SONI 2200910100019
P a g e | 20
Program to demonstrate constructor overloading.
Program Code:
class HelloConstructor3 {
public HelloConstructor3() {
[Link]("Hello Java");
public HelloConstructor3(String name) {
[Link]("Hello " + name);
public HelloConstructor3(int empId) {
[Link]("Hello " + empId);
public class HelloConstructor3Test {
public static void main(String args[]) {
new HelloConstructor3();
new HelloConstructor3("Rex");
new HelloConstructor3(2725);
AMISH SONI 2200910100019
P a g e | 21
Input/Output:
Hello Java
Hello Rex
Hello 2725
AMISH SONI 2200910100019
P a g e | 22
Program to demonstrate method in a class.
Program Code:
public class Big {
public static int max2(int a, int b) {
int result = a > b ? a : b;
return result;
public static void main(String args[]) {
Scanner sc = new Scanner([Link]);
[Link]("Enter two numbers one by one: ");
int a = [Link]();
int b = [Link]();
int result = max2(a, b);
[Link]("Biggest: " + result);
Input/Output:
Enter two numbers one by one: 2 3
Biggest: 3
AMISH SONI 2200910100019
P a g e | 23
Program to demonstrate arrays in Java.
Program Code:
public class StudentMarks {
public static void main(String args[]) {
final int MAX = 4;
int[] marks = new int[MAX];
if ([Link] != MAX) {
[Link]("Enter 4 marks");
return;
for (int i = 0; i < MAX; i++) {
marks[i] = [Link](args[i]);
int sum = 0;
for (int i = 0; i < MAX; i++) {
sum += marks[i];
[Link]("Average: " + (double)sum / MAX);
Input/Output:
javac [Link] 98 99 98 99
Average: 98.5
AMISH SONI 2200910100019