Skip to content

Commit

Permalink
java-prac-2-path-fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
rajjitlai committed Oct 15, 2024
1 parent 172f455 commit 8756ec2
Show file tree
Hide file tree
Showing 8 changed files with 78 additions and 66 deletions.
2 changes: 1 addition & 1 deletion Java/Practical/Practicals.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
- b. Multiply two complex numbers.
- c. toString() method to display complex numbers in the form: x + i y

2. Create a class TwoDim which contains private members as x and y coordinates in package Pl. Define the default constructor, a parameterized constructor and override toString() method to display the co-ordinates. Now reuse this class and in package P2 create another class ThreeDim, adding a new dimension as z as its private member. Define the constructors for the subclass and override toString() method in the subclass also. Write appropriate methods to show dynamic method dispatch. The main() function should be in a package P.
2. Create a class TwoDim which contains private members as x and y coordinates in package P1. Define the default constructor, a parameterized constructor and override toString() method to display the co-ordinates. Now reuse this class and in package P2 create another class ThreeDim, adding a new dimension as z as its private member. Define the constructors for the subclass and override toString() method in the subclass also. Write appropriate methods to show dynamic method dispatch. The main() function should be in a package P.

3. Define an abstract class Shape in package Pl. Inherit two more classes: Rectangle in package P2 and Circle in package P3 Write a program to ask the user for the type of shape and then using the concept of dynamic method dispatch, display the area of the appropriate subclass. Also write appropriate methods to read the data. The main() function should not be in any package.

Expand Down
31 changes: 31 additions & 0 deletions Java/Practical/practical-02/P/ProgramExample.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package P;

import P1.TwoDim;
import P2.ThreeDim;
import java.util.Scanner;

public class ProgramExample {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
TwoDim obj;

System.out.println("Choose type of coordinates:");
System.out.println("1. TwoDim (2D)");
System.out.println("2. ThreeDim (3D)");
int choice = sc.nextInt();

switch (choice) {
case 1:
obj = new P1.TwoDim(3, 4);
break;
case 2:
obj = new ThreeDim(5, 6, 7);
break;
default:
System.out.println("Invalid choice!");
return;
}

System.out.println(obj.toString());
}
}
21 changes: 21 additions & 0 deletions Java/Practical/practical-02/P1/TwoDim.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package P1;

public class TwoDim {
private int x;
private int y;

public TwoDim() {
this.x = 0;
this.y = 0;
}

public TwoDim(int x, int y) {
this.x = x;
this.y = y;
}

@Override
public String toString() {
return "TwoDim Coordinates: (" + x + ", " + y + ")";
}
}
25 changes: 25 additions & 0 deletions Java/Practical/practical-02/P2/ThreeDim.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package P2;

import P1.TwoDim;

public class ThreeDim extends TwoDim {
private int z;

// Default constructor
public ThreeDim() {
super();
this.z = 0;
}

// Parameterized constructor
public ThreeDim(int x, int y, int z) {
super(x, y);
this.z = z;
}

// Override toString() to display three-dimensional coordinates
@Override
public String toString() {
return "ThreeDim Coordinates: (" + super.toString().substring(18) + ", " + z + ")";
}
}
Binary file removed Java/Practical/practical-02/ProgramExample.class
Binary file not shown.
65 changes: 0 additions & 65 deletions Java/Practical/practical-02/ProgramExample.java

This file was deleted.

Binary file removed Java/Practical/practical-02/ThreeDim.class
Binary file not shown.
Binary file removed Java/Practical/practical-02/TwoDim.class
Binary file not shown.

0 comments on commit 8756ec2

Please sign in to comment.