[Go to site: main page, start]

0% found this document useful (0 votes)
7 views18 pages

Object Oriented Programming Using Java AA

The document contains a series of Java programs that demonstrate various concepts of Object-Oriented Programming, including class definitions, constructors, method overloading, and debugging techniques. Each program includes specific debugging tasks to correct errors related to variable scope, input handling, and object interactions. The document serves as a comprehensive guide for understanding and applying fundamental Java programming principles.

Uploaded by

sathvikkummari6
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views18 pages

Object Oriented Programming Using Java AA

The document contains a series of Java programs that demonstrate various concepts of Object-Oriented Programming, including class definitions, constructors, method overloading, and debugging techniques. Each program includes specific debugging tasks to correct errors related to variable scope, input handling, and object interactions. The document serves as a comprehensive guide for understanding and applying fundamental Java programming principles.

Uploaded by

sathvikkummari6
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Object Oriented Programming using Java

Debugging Questions
1. Debug the Java program demonstrating class definition, instance fields, constructors,
method overloading, static keyword, and user input
import [Link];

class Student
{
int rollNumber;
String studentName;
static String collegeName = "ABC College";

Student()
{
rollNumber = 0;
studentName = "Unknown";
}

Student(int r, String n)
{
rollNumber = r;
studentName = n;
}

void display()
{
[Link]("Roll Number : " + rollNumber);
[Link]("Student Name : " + studentName);
[Link]("College Name : " + collegeName);
}

void display(int extra)


{
[Link]("Extra Value : " + extra);
display();
}

public static void main(String args[])


{
Scanner sc = new Scanner([Link]);

[Link]("Enter Roll Number");


int r = [Link]();
[Link](); // clear buffer

[Link]("Enter Name");
String n = [Link]();
Student s1 = new Student(r, n);
Student s2 = new Student();

[Link](10);
[Link]();

if (r > 0)
{
[Link]("Valid Roll Number");
}
else
{
[Link]("Invalid Roll Number");
}

[Link]();
}
}

2. Debug the Java program illustrating primitive data types, operators, conditional
statements, object creation, and difference between local and instance variables
class Calculation
{
int a = 10;
int b = 20;

void calculate()
{
int a = 5; // local variable
int sum = a + b;
int diff = a - b;
int mul = a * b;
int div = a / b; // fixed division by zero

[Link]("Sum : " + sum);


[Link]("Difference : " + diff);
[Link]("Multiplication : " + mul);
[Link]("Division : " + div);
}

void compare()
{
if (a > b)
[Link]("A is greater");
else if (a == b)
[Link]("Both are equal");
else
[Link]("B is greater");
}

public static void main(String args[])


{
Calculation obj = new Calculation();
[Link]();
[Link]();

for (int i = 0; i <= 5; i++)


{
[Link]("Loop Value : " + i);
}

boolean result = (obj.a > 5) && (obj.b < 15);


[Link]("Logical Result : " + result);
[Link]("Program Completed");
}
}3. Debug the Java program demonstrating constructor overloading, static and final
keywords, Object class methods, and keyboard input
import [Link];

class Product
{
int productId;
String productName;
final double taxRate = 18.0;
static int count;

Product()
{
productId = 0;
productName = "None";
count++;
}

Product(int id, String name)


{
productId = id;
productName = name;
count = count + 1;
}

void showDetails()
{
[Link]("Product ID : " + productId);
[Link]("Product Name : " + productName);
[Link]("Tax Rate : " + taxRate);
[Link]("Object Hash : " + [Link]());
}

public static void main(String args[])


{
Scanner sc = new Scanner([Link]);

[Link]("Enter Product ID");


int id = [Link]();
[Link](); // clear buffer

[Link]("Enter Product Name");


String name = [Link]();

Product p1 = new Product(id, name);


Product p2 = new Product();

[Link]();
[Link]();

[Link]("Total Objects : " + count);

if (count >= 2)
{
[Link]("Multiple Objects Created");
}
else
{
[Link]("Single Object Created");
}

[Link]();
}
}4. Debug the Java program demonstrating JDK/JRE concepts, primitive data types,
operators, and conditional statements
class JavaBasics
{
int x = 25;
double y = 10.5;
boolean status = true;

void compute()
{
double result1 = x + y; // fixed type
double result2 = x / y; // fixed division by zero
int result3 = x % 2; // added semicolon

[Link]("Addition Result : " + result1);


[Link]("Division Result : " + result2);
[Link]("Modulus Result : " + result3);

if (x > y && status == true)


{
[Link]("Condition True");
}
else
{
[Link]("Condition False");
}
}

public static void main(String args[])


{
JavaBasics obj = new JavaBasics(); // added semicolon
[Link]();

for (int i = 1; i <= 5; i++)


{
if (i == 3)
break;

[Link]("Value : " + i);


}

[Link]("End of Program");
}
}

5. Debug the Java program illustrating class creation, instance variables, method calls, and
object parameter passing
class Employee
{
int empId;
String empName;
double salary;

void setData(int id, String name, double sal)


{
empId = id;
empName = name;
salary = sal;
}
void displayData()
{
[Link]("Employee ID : " + empId);
[Link]("Employee Name : " + empName);
[Link]("Salary : " + salary);
}

void updateSalary(Employee e)
{
[Link] = [Link] + 5000;
[Link]("Updated Salary : " + [Link]);
}

public static void main(String args[])


{
Employee emp = new Employee();
[Link](101, "Ravi", 25000);
[Link]();
[Link](emp);

if ([Link] > 30000)


{
[Link]("High Salary");
}
else
{
[Link]("Average Salary");
}

[Link]("Program Completed");
}
}

6. Debug the Java program demonstrating method overloading and constructor


overloading
class MathOperations
{
int value;

MathOperations()
{
value = 0;
}

MathOperations(int v)
{
value = v;
}

int add(int a, int b)


{
return a + b;
}

double add(double a, double b)


{
return a + b;
}

void display()
{
[Link]("Value : " + value);
}

public static void main(String args[])


{
MathOperations obj1 = new MathOperations();
MathOperations obj2 = new MathOperations(50);

int r1 = [Link](10, 20);


double r2 = [Link](10.5, 20.5);

[Link]();
[Link]();

[Link]("Result 1 : " + r1);


[Link]("Result 2 : " + r2);

if (r1 > r2)


[Link]("Integer Result Larger");
else
[Link]("Double Result Larger");
}
}

7. Debug the Java program demonstrating static and final keywords and difference
between local and instance variables
class Counter
{
static int count = 0;
static final int limit = 5; // made static
int value;
void increment()
{
value++;
count++;
}

void display()
{
[Link]("Value : " + value);
[Link]("Count : " + count);
[Link]("Limit : " + limit);
}

public static void main(String args[])


{
Counter c1 = new Counter();
Counter c2 = new Counter();

for (int i = 0; i < limit; i++)


{
[Link]();
[Link]();
}

[Link]();
[Link]();

[Link]("Final Count : " + count);


}
}

8. Debug the Java program illustrating Object class methods and reference comparison
class Sample
{
int num;
Sample(int n)
{
num = n
}
void show()
{
[Link]. class Sample
{
int num;

Sample(int n)
{
num = n;
}

void show()
{
[Link]("Number : " + num);
}

public static void main(String args[])


{
Sample s1 = new Sample(10);
Sample s2 = new Sample(10);

if (s1 == s2)
{
[Link]("Same Reference");
}
else
{
[Link]("Different Reference");
}

[Link]("Object 1 : " + [Link]());


[Link]("Object 2 : " + [Link]());

[Link]();
[Link]();

[Link]("Program Ends");
}
}

9. Debug the Java program demonstrating keyboard input and logical operators
import [Link];

class UserInputDemo
{
public static void main(String args[])
{
Scanner sc = new Scanner([Link]);

[Link]("Enter Age");
int age = [Link]();

[Link]("Enter Salary");
double salary = [Link]();
if (age >= 18 && salary > 20000 && salary < 50000)
{
[Link]("Eligible");
}
else
{
[Link]("Not Eligible");
}

[Link]("Age : " + age);


[Link]("Salary : " + salary);

[Link]();
}
}

10. Debug the Java program demonstrating comparison between Java and C/C++ concepts
using control structures
class LanguageComparison
{
int a = 10;
int b = 20;

void compare()
{
if (a == b) // fixed comparison operator
{
[Link]("Equal Values");
}
else
{
[Link]("Different Values");
}

while (a < b)
{
[Link]("A Value : " + a);
a++;
}
}

public static void main(String args[])


{
LanguageComparison obj = new LanguageComparison(); // added semicolon
[Link]();

[Link]("Comparison Done");
for (int i = 5; i >= 0; i--)
{
[Link](i);
}
}
}11. Debug the Java program demonstrating constructors, access modifiers, and object
creation
class Student
{
private int rollNo;
String name;
protected double marks;

Student()
{
rollNo = 0;
name = "Unknown";
marks = 0.0;
}

Student(int r, String n, double m)


{
rollNo = r;
name = n;
marks = m;
}

void display()
{
[Link]("Roll No : " + rollNo);
[Link]("Name : " + name);
[Link]("Marks : " + marks);
}

public static void main(String args[])


{
Student s1 = new Student();
Student s2 = new Student(101, "Anita", 85.5);

[Link]();
[Link]();

[Link]("Student Data Displayed");


}
}
12. Debug the Java program illustrating instance fields, local variables, and scope
class ScopeDemo
{
int value = 10;

void show()
{
int value = 20;
[Link]("Local Value : " + value);
[Link]("Instance Value : " + [Link]);
}

void update()
{
value = value + 5;
[Link]("Updated Instance Value : " + value);
}

public static void main(String args[])


{
ScopeDemo obj = new ScopeDemo();
[Link]();
[Link]();

if ([Link] > 10)


{
[Link]("Value Increased");
}
else
{
[Link]("Value Not Changed");
}

[Link]("End of Scope Demo");


}
}

13. Debug the Java program demonstrating use of static members and object interaction
class Company
{
static String companyName = "ABC Ltd";
int empId;

void setId(int id)


{
empId = id;
}
void display()
{
[Link]("Company : " + companyName);
[Link]("Employee ID : " + empId);
}

public static void main(String args[])


{
Company e1 = new Company();
Company e2 = new Company();

[Link](101);
[Link](102);

[Link]();
[Link]();

[Link] = "XYZ Pvt Ltd";

[Link]();
[Link]();

[Link]("Program Finished");
}
}

14. Debug the Java program demonstrating conditional and logical operators
class EligibilityCheck
{
int age;
double income;

void setData(int a, double i)


{
age = a;
income = i;
}

void check()
{
if (age >= 21 && income > 10000 && income <= 25000)
{
[Link]("Eligible");
}
else
{
[Link]("Not Eligible");
}
}

public static void main(String args[])


{
EligibilityCheck obj = new EligibilityCheck();
[Link](22, 18000);
[Link]();

[Link]("Age : " + [Link]);


[Link]("Income : " + [Link]);
[Link]("Check Completed");
}
}

15. Debug the Java program demonstrating keyboard input and primitive type conversion
import [Link];

class InputConversion
{
public static void main(String args[])
{
Scanner sc = new Scanner([Link]);

[Link]("Enter Integer Value");


int num = [Link]();

double result = num / 3.0; // use 3.0 to get double division


float value = (float) result;

[Link]("Result : " + result);


[Link]("Float Value : " + value);

if (result == value)
{
[Link]("Values Equal");
}
else
{
[Link]("Values Not Equal");
}

[Link]();
[Link]("Input Completed");
}
}

16. Debug the Java program demonstrating use of final keyword and constants
class Bank
{
final double RATE = 7.5; // final variable cannot be changed
double amount;

void setAmount(double amt)


{
amount = amt;
}

void calculateInterest()
{

double interest = amount * RATE / 100;


[Link]("Interest : " + interest);
}

public static void main(String args[])


{
Bank b = new Bank();
[Link](50000);
[Link]();

[Link]("Calculation Done");
}
}

17. Debug the Java program demonstrating method calls using objects as parameters
class Calculator
{
int value;

void setValue(int v)
{
value = v;
}

void add(Calculator c)
{
[Link] = [Link] + value;
}

void display()
{
[Link]("Value : " + value);
}

public static void main(String args[])


{
Calculator c1 = new Calculator();
Calculator c2 = new Calculator();

[Link](10);
[Link](20);

[Link](c2);

[Link]();
[Link]();

[Link]("Operation Completed");
}
}

18. Debug the Java program demonstrating use of Object class methods
class ObjectDemo
{
int id;

ObjectDemo(int i)
{
id = i;
}

public static void main(String args[])


{
ObjectDemo o1 = new ObjectDemo(1);
ObjectDemo o2 = new ObjectDemo(1);

[Link]([Link](o2));
[Link]([Link]());
[Link]([Link]());

if (o1 == o2)
[Link]("Same Object");
else
[Link]("Different Objects");

[Link]("Object Demo Finished");


}
}

19. Debug the Java program demonstrating nested conditional statements


class GradeCheck
{
int marks;

void setMarks(int m)
{
marks = m;
}

void calculateGrade()
{
if (marks >= 75)
[Link]("Distinction");
else if (marks >= 60)
[Link]("First Class");
else if (marks >= 50)
[Link]("Second Class");
else
[Link]("Fail");
}

public static void main(String args[])


{
GradeCheck g = new GradeCheck();
[Link](68);
[Link]();
[Link]("Grade Calculated");
}
}

20. Debug the Java program demonstrating operators and looping structures
class LoopOperatorDemo
{
public static void main(String args[])
{
int sum = 0;

for (int i = 1; i <= 10; i++)


{
sum = sum + i;
[Link]("i : " + i);
}

[Link]("Total : " + sum);


int j = 10;
while (j > 0)
{
[Link](j);
j--;
}

[Link]("Program Ends");
}
}

You might also like