Week 4: Programming Assignment 1
Understanding Default Access (No Keyword Used)
Problem Statement
In Java, variables and methods usually have an access level.
An access level controls where a variable can be used.
If no access keyword is written, Java applies default access automatically.
In beginner programs, all code is written in one file.
This means default access members can be used freely inside the same file.
In this program, you will:
Create a simple class.
Store one value inside it.
Access that value from the main method.
This program teaches how Java allows access without writing any modifier.
Input : 101
output: Roll Number is: 101
Please note that-
- The output of your program must match the expected output exactly, including capital letters, spaces, and line breaks.
- Presentation errors can be ignored and you will get full marks
import java.util.Scanner;
public class W04_P1 {
// Student class with default access variable
static class Student {
// No keyword written before int
// This means default access is applied
int rollNo;
// Constructor assigns value to rollNo
Student(int r) {
rollNo = r;
}
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
// Read roll number from user
int r = sc.nextInt();
// Create object of Student
Student s = new Student(r);
// BEGINNER INSTRUCTION:
// Access the roll number stored inside the object.
// Use the object name followed by dot operator.
// Do not create a new variable.
// Print exactly in the required format.
// TODO: Print the roll number here
System.out.print("Roll Number is: "+s.rollNo);
sc.close();
}
}
Week 4: Programming Assignment 2
Understanding Public Access Modifier
Problem Statement
In this assignment, you will demonstrate the use of a public variable in Java.
You are given a class Car inside the main class W04_P2.
The class Car contains a public integer variable named speed.
Your task is to:
Read one integer value from standard input.
Create an object of class Car.
Assign the input value to the public variable speed using the object.
Print the value in the exact format shown below.
Use dot notation to access the public variable.
Do not create any additional methods.
Input Format
A single integer representing the speed.
Output Format
Print the output exactly in the following format:
Speed is:
There must be one space after the colon.
Do not print any extra text or additional lines.
Sample Input
80
Sample Output
Speed is: 80
Disclaimer
To pass the test cases, the output of your program must match the expected output exactly, including case, spaces, and line breaks.
Presentation errors can be ignored and you will get full marks.
If a test case passes, you will receive marks even if there are minor presentation errors, since presentation does not affect evaluation of logic.
import java.util.Scanner;
public class W04_P2 {
// Car class with a public variable
static class Car {
// Public variable
public int speed;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
// Read speed value
int s = sc.nextInt();
// Create object of Car
Car c = new Car();
// Assign value to public variable
c.speed = s;
// BEGINNER INSTRUCTION:
// Access the public variable using object name.
// Print the value in required format.
// Public variables do not need special methods.
// TODO: Print the speed here
System.out.print("Speed is: "+c.speed);
sc.close();
}
}
Week 4: Programming Assignment 3
Understanding Private Access and Getter Method
Problem Statement
In this assignment, you will demonstrate the use of a private variable and a getter method in Java.
You are given a class Account inside the main class W04_P3.
The class Account contains a private integer variable named balance.
A method setBalance(int b) is already provided to assign a value to balance.
Your task is to:
Write a public method named getBalance() inside the Account class.
The method must return the value of the private variable balance.
Do not change the existing code.
Do not print anything inside the Account class.
The main method will:
Read an integer value from input.
Set the balance using setBalance(b).
Print the balance using the getter method.
Input Format
A single integer representing the account balance.
Output Format
Print the output exactly in the following format:
Account Balance is:
There must be one space after the colon.
Do not print any extra text or additional lines.
Sample Input
5000
Sample Output
Account Balance is: 5000
Disclaimer
To pass the test cases, the output of your program must match the expected output exactly, including case, spaces, and line breaks.
Presentation errors can be ignored and you will get full marks.
If a test case passes, you will receive marks even if there are minor presentation errors, since presentation does not affect evaluation of logic.
import java.util.Scanner;
public class W04_P3 {
static class Account {
// Private variable
private int balance;
// Method to set balance
public void setBalance(int b) {
balance = b;
}
// BEGINNER INSTRUCTION:
// Create a method to return balance.
// Method must be public.
// Return the private variable.
// TODO: Write getBalance method
public int getBalance() {
return balance;
}
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int b = sc.nextInt();
Account acc = new Account();
acc.setBalance(b);
System.out.println("Account Balance is: " + acc.getBalance());
sc.close();
}
}
Week 4: Programming Assignment 4
Understanding Protected Access with Inheritance
Problem Statement
Java allows sharing data between related classes.
The protected keyword supports inheritance.
A protected variable can be accessed:
Inside the same class.
Inside child classes.
In this program, you will:
Create a parent class.
Create a child class.
Access inherited data.
This introduces inheritance-based access.
In this assignment, you will demonstrate the use of a protected variable in Java using inheritance.
You are given a parent class Employee inside the main class W04_P4.
The class Employee contains a protected integer variable named salary.
You are also given a child class Manager that extends Employee.
Your task is to:
Read one integer value from standard input.
Create an object of class Manager.
Assign the input value to the inherited protected variable salary.
Print the value in the exact format shown below.
Access the variable directly inside the child class context.
Do not create additional methods.
Do not modify the class structure.
Input Format
A single integer representing the salary.
Output Format
Print the output exactly in the following format:
Salary is:
There must be one space after the colon.
Do not print any extra text or additional lines.
Sample Input
70000
Sample Output
Salary is: 70000
Disclaimer
To pass the test cases, the output of your program must match the expected output exactly, including case, spaces, and line breaks.
Presentation errors can be ignored and you will get full marks.
If a test case passes, you will receive marks even if there are minor presentation errors, since presentation does not affect evaluation of logic.
import java.util.Scanner;
public class W04_P4 {
static class Employee {
// Protected variable
protected int salary;
}
static class Manager extends Employee {
// Inherits salary
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int s = sc.nextInt();
Manager m = new Manager();
// BEGINNER INSTRUCTION:
// Assign value to inherited variable.
// Use object reference.
// Print the value.
// TODO: Assign and print salary
m.salary = s;
System.out.print("Salary is: "+m.salary);
sc.close();
}
}
Week 4: Programming Assignment 5
Access Modifiers with Method Overloading
Problem Statement
Java allows multiple methods with the same name.
This is called method overloading.
Access modifiers still apply to overloaded methods.
In this program, you will:
Create two methods with same name.
Use public access for one.
Use private access for another.
This teaches method visibility with overloading.
You are given a class Calculator inside the main class W04_P5.
One public method add(int a, int b) is already defined.
This method returns the sum of two integers.
Your task is to:
Write another method named add inside the Calculator class.
The new method must accept three integer parameters.
The new method must be declared as private.
It must return the sum of the three integers.
Do not modify the existing public add(int a, int b) method.
The method printThreeSum(int x, int y, int z) will call your private method and print the result.
Do not change the given structure of the program.
Input Format
Four integers will be given as input:
First two integers for addition of two numbers.
Next three integers for addition of three numbers.
Output Format
Print the output exactly in the following format:
Sum of two numbers:
Sum of three numbers:
There must be one space after the colon.
Each output must be printed on a new line.
Do not print any extra text.
Sample Input
5
10
2
3
4
Sample Output
Sum of two numbers: 15
Sum of three numbers: 9
Disclaimer
To pass the test cases, the output of your program must match the expected output exactly, including case, spaces, and line breaks.
Presentation errors can be ignored and you will get full marks.
If a test case passes, you will receive marks even if there are minor presentation errors, since presentation does not affect evaluation of logic.
import java.util.Scanner;
public class W04_P5 {
static class Calculator {
// Public method
public int add(int a, int b) {
return a + b;
}
// BEGINNER INSTRUCTION:
// Write another add method.
// Use private keyword.
// Accept three integers.
// Return their sum.
// TODO: Write private add method
private int add(int a, int b,int c) {
return a + b + c;
}
public void printThreeSum(int x, int y, int z) {
int sum = add(x, y, z);
System.out.println("Sum of three numbers: " + sum);
}
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int a = sc.nextInt();
int b = sc.nextInt();
int x = sc.nextInt();
int y = sc.nextInt();
int z = sc.nextInt();
Calculator calc = new Calculator();
System.out.println("Sum of two numbers: " + calc.add(a, b));
calc.printThreeSum(x, y, z);
sc.close();
}
}
Previous :Week 4 - Assignment