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

Java 1 W

The document contains several Java programs demonstrating various programming concepts, including class creation, constructors (default, parameterized, and overloaded), methods, and arrays. Each program is accompanied by its code and input/output examples. The author of the document is Devansh Bansal, with a student ID provided.

Uploaded by

bdevansh36
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views8 pages

Java 1 W

The document contains several Java programs demonstrating various programming concepts, including class creation, constructors (default, parameterized, and overloaded), methods, and arrays. Each program is accompanied by its code and input/output examples. The author of the document is Devansh Bansal, with a student ID provided.

Uploaded by

bdevansh36
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

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");

Devansh Bansal 2200910100057


7
}

//[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

Devansh Bansal 2200910100057


7
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

Devansh Bansal 2200910100057


7
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

Devansh Bansal 2200910100057


7
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);

Devansh Bansal 2200910100057


7
Input/Output:

Hello Java

Hello Rex

Hello 2725

Devansh Bansal 2200910100057


7
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

Devansh Bansal 2200910100057


7
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

Devansh Bansal 2200910100057


7

You might also like