Static Non Static
Static Non Static
Static Non Static
// static block
static
{
System.out.println("Inside static block");
}
// static method
static int m1()
{
System.out.println("from m1");
return 20;
}
Output:
Exercise 2:
OBJECT ORIENTED PROGRAMMING
/* Driver Code */
public static void main(String args[])
{
Studentsrecords s = new Studentsrecords("Monica");
s.setAge(14);
s.setDiv("B");
s.printstud();
}
}
Output:
Task 1: You are tasked with creating a Java class representing a student. The class should
include instance variables studentId, studentName, and grade. Implement a parameterized
constructor to initialize these variables. Provide a method to set the grade of the student, and
another method to display the student's information including ID, name, and grade. Write a Java
program to demonstrate the functionality of this class.
Code:
class Student {
private int studentID;
private String studentName;
private String grade;
Output:
Task 2: You are tasked with creating a Java class to represent a bookstore. The Bookstore class
should have the following components:
a. A static variable totalBooks to keep track of the total number of books available in the
bookstore.
b. A static method addBook(int quantity) that increments the total number of books by the
specified quantity.
c. A static method removeBook(int quantity) that decrements the total number of books by
the specified quantity.
d. A static method displayTotalBooks() that displays the current total number of books
available in the bookstore.
e. A static method resetBookstore() that resets the total number of books to zero.
Your task is to implement the Bookstore class with the given components and write a Java
program to demonstrate the functionality of the class. Additionally, provide a way to change the
value of the static variable totalBooks from outside the class.
Code:
Bookstore.java
public class Bookstore {
private static int totalBooks = 0;
Main2.java
// Main.java
public class Main2 {
public static void main(String[] args) {
Bookstore.addBook(150);
Bookstore.displayTotalBooks();
Bookstore.removeBook(120);
Bookstore.displayTotalBooks();
Bookstore.resetBookstore();
Bookstore.displayTotalBooks();
Bookstore.setTotalBooks(130);
Bookstore.displayTotalBooks();
}
}
Output:
Task 3: Create a Java class named ShoppingCart to represent a shopping cart. This class should
have instance variables: items, an array to store items, and totalItems, an integer representing
the total number of items in the cart. Implement a parameterized constructor to initialize the
items array with a specified size. Additionally, provide instance methods addItem(String item)
to add items to the cart and displayCart() to display the items currently in the cart. Write a Java
program to demonstrate the functionality of the ShoppingCart class by adding items to the cart
and displaying them.
Code:
class ShoppingCart {
private String[] items;
private int totalItems;
}
}
cart.addItem("Shoes");
cart.addItem("Perfume");
cart.addItem("Shirt");
cart.addItem("Chocolate");
cart.addItem("Chips");
cart.displayCart();
}
}
Output: