Javapart A
Javapart A
Output:
3. B. Implement a Java program to showcase loops.
System.out.printf("%4d", i);
System.out.println("\n----------------------------------------");
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:
width = w;
height = h;
depth = d;
Box()
Box(double len)
{
double volume()
double vol;
vol = mybox1.volume();
vol = mybox2.volume();
vol = mycube.volume();
Output:
String brand;
int year;
Vehicle(String b, int y)
brand = b;
year = y;
void drive()
int mileage;
super(b, y);
mileage = m;
void displayDetails()
myCar1.drive();
myCar1.displayDetails();
myCar2.drive();
myCar2.displayDetails();
Output:
class Person{
String name;
Person(String n)
interface Mother
void FeedChildren();
interface Wife
{
void CallHusband();
WifeAndMother(String n)
super(n);
class Testing
w.FeedChildren();
w.CallHusband();
}
Output:
A.java:
B.java:
OUTPUT: