W09 Programming Assignments 1
Due date on 2026-09-24, 23:59 IST
Javaimport java.util.Scanner;
public class W09_P1 {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
// Declare the 5X5 2D array to store the input
char original[][] = new char[5][5];
// Input 2D Array using Scanner Class and check data validity
for (int line = 0; line < 5; line++) {
String input = sc.nextLine();
char seq[] = input.toCharArray();
if (seq.length == 5) {
for (int i = 0; i < 5; i++) {
if (seq[i] == '0' || seq[i] == '1') {
original[line][i] = seq[i];
if (line == 4 && i == 4)
flipflop(original);
} else {
System.out.print("Only 0 and 1 supported.");
break;
}
}
} else {
System.out.print("Invalid length");
break;
}
}
} // The main() ends here
static void flipflop(char[][] flip) {
// Flip-Flop Operation
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 5; j++) {
if (flip[i][j] == '1')
flip[i][j] = '0';
else
flip[i][j] = '1';
}
}
// Output the 2D FlipFlopped Array without a trailing newline after the 5th line
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 5; j++) {
System.out.print(flip[i][j]);
}
if (i < 4) {
System.out.println();
}
}
}
} // The main class ends here
import java.util.Scanner;
public class W09_P1 {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
// Declare the 5X5 2D array to store the input
char original[][] = new char[5][5];
// Input 2D Array using Scanner Class and check data validity
for (int line = 0; line < 5; line++) {
String input = sc.nextLine();
char seq[] = input.toCharArray();
if (seq.length == 5) {
for (int i = 0; i < 5; i++) {
if (seq[i] == '0' || seq[i] == '1') {
original[line][i] = seq[i];
if (line == 4 && i == 4)
flipflop(original);
} else {
System.out.print("Only 0 and 1 supported.");
break;
}
}
} else {
System.out.print("Invalid length");
break;
}
}
} // The main() ends here
static void flipflop(char[][] flip) {
// Flip-Flop Operation
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 5; j++) {
if (flip[i][j] == '1')
flip[i][j] = '0';
else
flip[i][j] = '1';
}
}
// Output the 2D FlipFlopped Array without a trailing newline after the 5th line
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 5; j++) {
System.out.print(flip[i][j]);
}
if (i < 4) {
System.out.println();
}
}
}
} // The main class ends here
This assignment has Public Test cases. Please click on "Compile & Run" button to see the status of Public test cases. Assignment will be evaluated only after submitting using Submit button below. If you only save as or compile and run the Program, your assignment will not be graded and you will not see your score after the deadline.
Evaluation Results
Note: These tests may not be considered while scoring.
Due date on 2026-09-24, 23:59 IST
import java.util.Scanner;
public class W09_P2 {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
String input = sc.nextLine(); // Read as string, e.g., 5+6
// Declare and initialize the required variable(s)
int i = 0;
int j = 0;
double output = 0;
// Split the input string into character array
char seq[] = input.toCharArray();
/*
Use some method to separate the two operands
and then perform the required operation.
*/
for (int a = 0; a < seq.length; a++) {
if (seq[a] == '+') {
i = Integer.parseInt(input.substring(0, a));
j = Integer.parseInt(input.substring(a + 1, seq.length));
output = (double) i + j;
} else if (seq[a] == '-') {
i = Integer.parseInt(input.substring(0, a));
j = Integer.parseInt(input.substring(a + 1, seq.length));
output = (double) i - j;
} else if (seq[a] == '/') {
i = Integer.parseInt(input.substring(0, a));
j = Integer.parseInt(input.substring(a + 1, seq.length));
output = (double) i / j;
} else if (seq[a] == '*') {
i = Integer.parseInt(input.substring(0, a));
j = Integer.parseInt(input.substring(a + 1, seq.length));
output = (double) i * j;
}
}
System.out.print(input + " = " + Math.round(output));
} // The main() method ends here
} // The main class ends here
This assignment has Public Test cases. Please click on "Compile & Run" button to see the status of Public test cases. Assignment will be evaluated only after submitting using Submit button below. If you only save as or compile and run the Program, your assignment will not be graded and you will not see your score after the deadline.
Evaluation Results
Note: These tests may not be considered while scoring.
Due date on 2026-09-24, 23:59 IST
import java.util.Scanner;
class SquareThread extends Thread {
private int begin;
private int end;
public SquareThread(int begin, int end) {
this.begin = begin;
this.end = end;
}
public synchronized void run() {
if (begin > end) {
for (int i = begin; i >= end; i--) {
System.out.println(i * i);
}
} else {
for (int i = begin; i <= end; i++) {
System.out.println(i * i);
}
}
}
}
public class W09_P3 {
public static void main(String args[]) throws InterruptedException {
Scanner scanner = new Scanner(System.in);
int begin = scanner.nextInt();
int end = scanner.nextInt();
scanner.close();
SquareThread thread1 = new SquareThread(begin, end);
SquareThread thread2 = new SquareThread(end, begin);
thread1.start();
thread1.join();
thread2.start();
}
}
This assignment has Public Test cases. Please click on "Compile & Run" button to see the status of Public test cases. Assignment will be evaluated only after submitting using Submit button below. If you only save as or compile and run the Program, your assignment will not be graded and you will not see your score after the deadline.
Evaluation Results
Note: These tests may not be considered while scoring.
Due date on 2026-09-24, 23:59 IST
import java.io.*;
class W09_P4 {
public static void main(String args[]) {
try {
java.util.Scanner r = new java.util.Scanner(System.in);
String number = r.nextLine();
int x = Integer.parseInt(number);
System.out.print(x * x);
} catch (Exception e) {
System.out.print("Please enter valid data");
}
}
}
This assignment has Public Test cases. Please click on "Compile & Run" button to see the status of Public test cases. Assignment will be evaluated only after submitting using Submit button below. If you only save as or compile and run the Program, your assignment will not be graded and you will not see your score after the deadline.
Evaluation Results
Note: These tests may not be considered while scoring.
Due date on 2026-09-24, 23:59 IST
import java.util.Scanner;
public class W09_P5 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
double x1 = sc.nextDouble();
double y1 = sc.nextDouble();
double x2 = sc.nextDouble();
double y2 = sc.nextDouble();
Point p1 = new Point(x1, y1);
Point p2 = new Point(x2, y2);
System.out.print(p1.distance(p2));
}
}
class Point {
private double x;
private double y;
public Point(double x, double y) {
this.x = x;
this.y = y;
}
public double distance(Point p2) {
double d;
d = Math.sqrt((p2.x - x) * (p2.x - x) + (p2.y - y) * (p2.y - y));
return d;
}
}
This assignment has Public Test cases. Please click on "Compile & Run" button to see the status of Public test cases. Assignment will be evaluated only after submitting using Submit button below. If you only save as or compile and run the Program, your assignment will not be graded and you will not see your score after the deadline.
Evaluation Results
Note: These tests may not be considered while scoring.
No comments