You are learning Java programming for the first time. In this problem, you will calculate the area of a rectangle. A rectangle has two values: length and width. The area tells how much space the rectangle covers. The formula to calculate area is: Area = length multiplied by width. You must read both values from the user. You must calculate the area using the formula. Finally, you must print the result exactly as required.
import java.util.Scanner; public class W02_P1 { public static void main(String[] args) { // Create Scanner object to read input from keyboard Scanner sc = new Scanner(System.in); // Read the length of the rectangle int length = sc.nextInt(); // Read the width of the rectangle int width = sc.nextInt(); // TODO: Write the area calculation here int area=length*width; // Print the calculated area System.out.println("Area is: " + area); // Close the scanner sc.close(); } }Explanation Click here to wtach the video
Learning Operator Precedence Problem Statement This exercise teaches how Java evaluates expressions. You will work with more than one arithmetic operator. You will understand why brackets are important. Incorrect placement of brackets can change the result. The program receives two numbers. You must combine them using addition and multiplication. The focus of this question is expression formation, not input.
import java.util.Scanner; public class W02_P2 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int length = sc.nextInt(); int width = sc.nextInt(); // BEGINNER GUIDANCE: // First combine both values. // Then apply multiplication. // Use brackets to control execution order. // // Without brackets, result will be wrong. // TODO: Write the expression correctly int perimeter=2*(length+width); System.out.println("Perimeter is: " + perimeter); sc.close(); } }Explanation Click here to wtach the video
Learning Arrays and Indexing Problem Statement This exercise introduces collections of data. Instead of storing one value, you store many values together. Java uses arrays for this purpose. Each value is stored at a numbered position. You will read multiple numbers. You will store them in an array. You will inspect each value carefully. Your task is to identify the highest value. This problem focuses on array indexing and comparison.
import java.util.Scanner; public class W02_P3 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int[] arr = new int[n]; for (int i = 0; i < n; i++) { arr[i] = sc.nextInt(); } int max = arr[0]; // BEGINNER INSTRUCTION: // Look at each value one by one. // Compare it with the stored maximum. // Replace maximum only if current value is larger. // // Remember: // Array indexing starts from 0. // TODO: Complete comparison logic for (int i = 1; i < n; i++) { if(max<arr[i]){ max=arr[i]; } } System.out.println("Maximum is: " + max); sc.close(); } }Explanation Click here to wtach the video
Learning Classes and Objects Problem Statement This exercise introduces object-oriented thinking. A class groups related data. An object stores real values using that structure. You will create a simple class. You will store values inside an object. You will access those values using dot notation. This problem focuses only on class creation and object usage.
import java.util.Scanner; public class W02_P4 { static class Rectangle { int length; int width; } public static void main(String[] args) { Scanner sc = new Scanner(System.in); int l = sc.nextInt(); int w = sc.nextInt(); Rectangle rect = new Rectangle(); rect.length = l; rect.width = w; // NOVICE NOTE: // Access values using object name. // Do not use input variables directly. // Combine values stored inside the object. // TODO: Print the required output System.out.print("Sum of length and width is: "+ (rect.length+ rect.width)); sc.close(); } }Explanation Click here to wtach the video
Learning Constructors and Object Initialization Problem Statement This exercise teaches how objects receive values automatically. A constructor runs at the time of object creation. It removes the need for manual assignment. The this keyword clearly distinguishes object data from input data. You will create a class with one variable. You will initialize it using a constructor. You will verify the value by printing it. This problem focuses only on constructor logic.
Previous :Week 2 - Assignment Next :Week 3 - Assignmentimport java.util.Scanner; public class W02_P5 { static class Circle { int radius; // BEGINNER INSTRUCTION: // Constructor name must match class name. // Parameter and variable have same name. // Use 'this' to avoid confusion. // TODO: Write constructor here Circle(int radius) { this.radius=radius; } } public static void main(String[] args) { Scanner sc = new Scanner(System.in); int r = sc.nextInt(); Circle c = new Circle(r); System.out.println("Radius of the circle is: " + c.radius); sc.close(); } }Explanation Click here to wtach the video