0% found this document useful (0 votes)
28 views10 pages

Javapart A

A

Uploaded by

SACHIN
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)
28 views10 pages

Javapart A

A

Uploaded by

SACHIN
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/ 10

PART-A

2. Perform basic arithmetic operations using different operators.

public class ArithmeticOperations


{
public static void main(String[] args) {
int num1 = 10, num2 = 5, value = 10, x = 5;
System.out.println("Sum: " + (num1 + num2));
System.out.println("Difference: " + (num1 - num2));
System.out.println("Product: " + (num1 * num2));
System.out.println("Quotient: " + (num1 / num2));
System.out.println("Remainder: " + (num1 % num2));
System.out.println("Value after increment: " + (++value));
System.out.println("Value after decrement: " + (--value));
x += 3;
System.out.println("Value of x after addition: " + x);
x *= 2;
System.out.println("Value of x after multiplication: " + x);
System.out.println("Result of integer division: " + ((double) num1 / num2));
}
}
Output:
3. A. Implement a Java program to showcase decision-making structures
and loops.
import java.util.Scanner;
public class Calculator {
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter two numbers: ");
int a = sc.nextInt();
int b = sc.nextInt();
System.out.print("Enter an operation (+, -, *, /, %): ");
char op = sc.next().charAt(0);
System.out.println("Result: " + solve(a, b, op));
}
public static int solve(int a, int b, char op)
{
switch (op)
{
case '+': return a + b;
case '-': return a - b;
case '*': return a * b;
case '/': return a / b;
case '%': return a % b;
default: throw new IllegalArgumentException("Invalid operation");
}
}
}

Output:
3. B. Implement a Java program to showcase loops.

public class MultiplicationTable

public static void main(String[] args)

int size = 10;

// Print header row

System.out.print(" "); // Alignment for numbers

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

System.out.printf("%4d", i);

System.out.println("\n----------------------------------------");

// Print table body

for (int i = 1; i <= size; i++)

System.out.printf("%2d |", i);

for (int j = 1; j <= size; j++)

System.out.printf("%4d", i * j);

System.out.println();

Output:
4. Define a class with methods and variables, create objects, and access
class members

class MyClass {
int x;
String name;
MyClass(int x, String name) {
this.x = x;
this.name = name;
}
void display() {
System.out.println("Name: " + name + "\nValue of x: " + x);
}
void setName(String newName) {
name = newName;
}
}
public class Main {
public static void main(String[] args) {
MyClass obj1 = new MyClass(10, "Object 1"), obj2 = new MyClass(20, "Object 2");
obj1.display();
obj1.setName("Updated Object 1");
obj1.display();
obj2.display();
obj2.setName("Updated Object 2");
obj2.display();
}
}

Output:
5. A. Develop a program to demonstrate constructors and method
overloading.

import java.io.*;
class MethodOverloadingEx
{
static int add(int a, int b)
{
return a + b;
}
static int add(int a, int b, int c)
{
return a + b + c;
}
public static void main(String args[]) {
System.out.println("add() with 2 parameters: " + add(4, 6));
System.out.println("add() with 3 parameters: " + add(4, 6, 7));
}
}

Output:

5. B. Develop a program to demonstrate constructors overloading.


class Box

double width, height, depth;

Box(double w, double h, double d)

width = w;

height = h;

depth = d;

Box()

width = height = depth = 0;

Box(double len)
{

width = height = depth = len;

double volume()

return width * height * depth;

public class Test

public static void main(String args[])

Box mybox1 = new Box(10, 20, 15);

Box mybox2 = new Box();

Box mycube = new Box(7);

double vol;

vol = mybox1.volume();

System.out.println("Volume of mybox1 is " + vol);

vol = mybox2.volume();

System.out.println("Volume of mybox2 is " + vol);

vol = mycube.volume();

System.out.println("Volume of mycube is " + vol);

Output:

6. Create a base class and a derived class, showcasing inheritance.


class Vehicle

String brand;

int year;

Vehicle(String b, int y)

brand = b;

year = y;

void drive()

System.out.println("Driving " + brand);

class Car extends Vehicle {

int mileage;

Car(String b, int y, int m) {

super(b, y);

mileage = m;

void displayDetails()

System.out.println("Brand: " + brand);

System.out.println("Year: " + year);

System.out.println("Mileage: " + mileage + " km/l");

public class InheritanceDemo

public static void main(String[] args)


{

Car myCar1 = new Car("Toyota", 2020, 20);

myCar1.drive();

myCar1.displayDetails();

Car myCar2 = new Car("Swift", 2023, 10);

myCar2.drive();

myCar2.displayDetails();

Output:

7. Implement interfaces to demonstrate multiple inheritance.

class Person{

String name;

Person(String n)

name = "Person: " + n;

interface Mother

void FeedChildren();

interface Wife
{

void CallHusband();

class WifeAndMother extends Person implements Wife, Mother

WifeAndMother(String n)

super(n);

name = "Wife and mother: " + n;

public void FeedChildren()

System.out.println(name + " is feeding the children.");

public void CallHusband()

System.out.println(name + " is calling her husband.");

class Testing

public static void main(String args[]) {

Person p = new Person("SreeRam");

WifeAndMother w = new WifeAndMother("Seetha");

System.out.println("p is a " + p.name);

System.out.println("w is a " + w.name);

w.FeedChildren();

w.CallHusband();

}
Output:

8. Write a java program to create user defined java package.

A.java:

package pack;public class A {


public void msg() {
System.out.println("Hello");
}
}

B.java:

package pack;public class B {


public static void main(String args[]) {
A obj = new A();
obj.msg();
}
}

OUTPUT:

You might also like