Oopj Assignment
Oopj Assignment
Cse 40 2205972
OOPJ ASSIGNMENT 1
Q. There are 5 self-driven marbles in the concentric circles given below. The diameter of the total circle is 40 em and width of
each track is 2 cm. The speed of the marble is (1.5 +0.1"t)cm/s, where t represents the track number.
(a) Write a program in Java to generate the position of each marble after every 2s, if initially each marble was top vertical
position initially.
Solution:
Input:
public class SelfDrivenMarbles1 {
public static void main(String[] args) {
double diameter = 40.0;
double trackWidth = 2.0;
int numTracks = (int) (diameter / trackWidth);
Solution:
Input:
public class Main {
public static void main(String[] args) {
int totalTracks = 5;
double diameter = 40.0;
double trackWidth = 2.0;
double[] speeds = new double[totalTracks];
boolean[] directions = new boolean[totalTracks];
Output:
Time: 1.0999999999999999s
Marble 1 position: 1.65cm
Marble 2 position: 123.90370614359172cm
Marble 3 position: 1.8699999999999997cm
Marble 4 position: 123.68370614359172cm
Marble 5 position: 2.09cm
Time: 2.0000000000000004s
Marble 1 position: 3.000000000000001cm
Marble 2 position: 122.46370614359172cm
Marble 3 position: 3.400000000000001cm
Marble 4 position: 122.06370614359173cm
Marble 5 position: 3.8000000000000007cm
Time: 3.0000000000000013s
Marble 1 position: 4.500000000000002cm
Marble 2 position: 120.86370614359173cm
Marble 3 position: 5.100000000000002cm
Marble 4 position: 120.26370614359172cm
Marble 5 position: 5.700000000000002cm
Time: 4.000000000000002s
Marble 1 position: 6.000000000000003cm
Marble 2 position: 119.26370614359172cm
Marble 3 position: 6.8000000000000025cm
Marble 4 position: 118.46370614359172cm
Marble 5 position: 7.600000000000003cm
Time: 5.099999999999998s
Marble 1 position: 7.649999999999997cm
Marble 2 position: 117.50370614359173cm
Marble 3 position: 8.669999999999996cm
Marble 4 position: 116.48370614359173cm
Marble 5 position: 9.689999999999996cm
(C) Find the condition when each ball will move in a such way that they will fall in same line.
Solution:
Input:
public class Main {
public static void main(String[] args) {
int totalTracks = 5;
double[] speeds = new double[totalTracks];
double[] trackRadii = new double[totalTracks];
if (sameLine) {
System.out.println("All balls will fall in the same line.");
System.out.println("Time taken for all balls to fall in the same line: " + times[0] + " seconds");
} else {
System.out.println("All balls will not fall in the same line.");
}
}
}
Output:
All balls will not fall in the same line.