Question One
Find the issues in the following Java code, then correct them.
public class Rectangular
{
private int length;
private int width;
public Rectangular(int length, int width)
{
this.length = length;
this.width = width;
}
public String area ()
{
return length * width;
}
}
public class RectangularTest
{
public static void main (String[] args)
{
Rectangular r1 = new Rectangular(3,5);
System.out.println(area());
}
}
Question Two
1- Create a Java class for Student with the following requirements:
Each student has two attributes: Name and ID.
Create two constructors. One constructor without parameters to initialize all the instance variables to default values, and another constructor to initialize all the attributes to specific values.
Add all setter and getter methods.
2- Create a tester class with the main method with the following requirements.
Create two objects from Student class. Create the first object using the default constructor and the second object must set your name and ID.
Print your name and ID using getter methods.
Sample of the output:
Question Three
Suppose you have the following 2 dimensions array:
int arr[][] = {
{ 10, 11, 12, 13, 14 },
{ 15, 16, 17, 18, 19 },
{ 20, 21, 22, 23, 24 },
{ 25, 26, 27, 28, 29 },
{ 30, 31, 32, 33, 34 }
};
With the the following rows and columns sizes:
static int rows= 5;
static int columns= 5;
Write a Java programs that uses takes arr[][] and reverse (mirror) all rows. For example, the first row should be as the following after you reverse it 14 13 12 11 10. A screenshot of your output should also be included in your answer and should display both the original array and the reversed array.
Sample output:
Question Four
What is the output of the following Java program?
public class Exam
{
static int studyingHours = 31;
private String course = OOP;
public void compute(int studyingHours)
{
Exam e = new Exam();
this.studyingHours = 24;
course = Math;
System.out.println("Exam.studyingHours: " + Exam.studyingHours);
System.out.println("e.studyingHours: " + e.studyingHours);
System.out.println("e.course: " + e.course);
System.out.println("course: " + course);
}
public static void main(String args[])
{
Exam e = new Exam();
e.compute(31);
}
}
Question One: Rectangular Class Issues and Corrections
public class Rectangular {
private int length;
private int width;
public Rectangular(int length, int width) {
this.length = length;
this.width = width;
}
public int area() {
return length * width;
}
}
public class RectangularTest {
public static void main(String[] args) {
Rectangular r1 = new Rectangular(3, 5);
System.out.println(r1.area());
}
}
Question Two: Student Class and Tester Class
public class Student {
private String name;
private int id;
// Constructor without parameters
public Student() {
this.name = "Default Name";
this.id = 0;
}
// Constructor with specific values
public Student(String name, int id) {
this.name = name;
this.id = id;
}
// Getter and setter methods
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
}
public class StudentTester {
public static void main(String[] args) {
Student student1 = new Student();
Student student2 = new Student("Your Name", 12345);
System.out.println("Student 1 - Name: " + student1.getName() + ", ID: " + student1.getId());
System.out.println("Student 2 - Name: " + student2.getName() + ", ID: " + student2.getId());
}
}
Question Three: Reversing Rows in a 2D Array
public class ReverseArrayRows {
static int rows = 5;
static int columns = 5;
public static void main(String[] args) {
int[][] arr = {
{10, 11, 12, 13, 14},
{15, 16, 17, 18, 19},
{20, 21, 22, 23, 24},
{25, 26, 27, 28, 29},
{30, 31, 32, 33, 34}
};
// Reversing rows
for (int i = 0; i < rows; i++) {
for (int j = columns - 1; j >= 0; j--) {
System.out.print(arr[i][j] + " ");
}
System.out.println();
}
}
}
Question Four: Java Program Output Explanation
The given Java program has several issues:
1. The variable OOP and Math should be enclosed in double quotes to be treated as strings.
2. The compute method creates a new Exam object within itself unnecessarily.
Corrected Java Program:
public class Exam {
static int studyingHours = 31;
private String course = "OOP";
public void compute(int studyingHours) {
this.studyingHours = 24;
course = "Math";
System.out.println("Exam.studyingHours: " + Exam.studyingHours);
System.out.println("e.studyingHours: " + studyingHours); // Changed to local variable studyingHours
System.out.println("e.course: " + course);
System.out.println("course: " + course);
}
public static void main(String[] args) {
Exam e = new Exam();
e.compute(31);
}
}
The corrected version should provide the following output:
Exam.studyingHours: 24
e.studyingHours: 31
e.course: Math
course: Math
These corrections address the issues in the provided Java code snippets and ensure proper functionality and output alignment with the intended logic.