Static Non Static

Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 7

Experiment 12

OBJECT : To understand and create static and non-static variables/methods in a class.


Exercise 1:
// Java program to demonstrate execution
// of static blocks and variables
class Test {
// static variable
static int a = m1();

// static block
static
{
System.out.println("Inside static block");
}

// static method
static int m1()
{
System.out.println("from m1");
return 20;
}

// static method(main !!)


public static void main(String[] args)
{
System.out.println("Value of a : " + a);
System.out.println("from main");
}
}

Output:

Exercise 2:
OBJECT ORIENTED PROGRAMMING

public class Studentsrecords


{
/* declaration of instance variables. */
public String name; //public instance
String division; //default instance
private int age; //private instance
/* Constructor that initialize an instance variable. */
public Studentsrecords(String sname)
{
name = sname;
}

/* Method to initialize an instance variable. */


public void setDiv(String sdiv)
{
division = sdiv;
}

/* Method to initialize an instance variable. */


public void setAge(int sage)
{
age = sage;
}

/* Method to display the values of instance variables. */


public void printstud()
{
System.out.println("Student Name: " + name );
System.out.println("Student Division: " + division);
System.out.println("Student Age: " + age);
}

/* Driver Code */
public static void main(String args[])
{
Studentsrecords s = new Studentsrecords("Monica");
s.setAge(14);
s.setDiv("B");
s.printstud();
}
}

COLLEGE OF COMPUTER SCIENCE AND INFORMATION SYSTEMS


OBJECT ORIENTED PROGRAMMING

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;

public Student(int studentID, String studentName) {


this.studentID = studentID;
this.studentName = studentName;
}

public void setGrade(String grade) {


this.grade = grade;
}

public void displayInfo() {


System.out.println("Student ID: " + studentID);
System.out.println("Student Name: " + studentName);

COLLEGE OF COMPUTER SCIENCE AND INFORMATION SYSTEMS


OBJECT ORIENTED PROGRAMMING

System.out.println("Grade: " + grade);


}
}

public class Main {


public static void main(String[] args) {
Student student1 = new Student(34934, "Ramsha Imran");
student1.setGrade("A+");
System.out.println("Student 1 Information:");
student1.displayInfo();

Student student2 = new Student(34657, "Zara Khan");


student2.setGrade("A-");
System.out.println("\nStudent 2 Information:");
student2.displayInfo();
}
}

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.

COLLEGE OF COMPUTER SCIENCE AND INFORMATION SYSTEMS


OBJECT ORIENTED PROGRAMMING

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;

public static void addBook(int quantity) {


totalBooks= totalBooks+ quantity;
}

public static void removeBook(int quantity) {


if (totalBooks >= quantity) {
totalBooks= totalBooks - quantity;
} else {
System.out.println("Not enough books available to remove.");
}
}

public static void displayTotalBooks() {


System.out.println("Total books available: " + totalBooks);
}

public static void resetBookstore() {


totalBooks = 0;
System.out.println("Bookstore reset successful.");
}
public static void setTotalBooks(int newValue) {
totalBooks = newValue;
}
}

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();

COLLEGE OF COMPUTER SCIENCE AND INFORMATION SYSTEMS


OBJECT ORIENTED PROGRAMMING

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;

public ShoppingCart(int size) {


items = new String[size];
totalItems = 0;
}

public void addItem(String item) {


if (totalItems < items.length) {
items[totalItems] = item;
totalItems++;
System.out.println(item + " added to the cart.");
} else {
System.out.println("The cart is full. You cannot add more items.");

COLLEGE OF COMPUTER SCIENCE AND INFORMATION SYSTEMS


OBJECT ORIENTED PROGRAMMING

}
}

public void displayCart() {


System.out.println("Items in the cart:");
for (int i = 0; i < totalItems; i++) {
System.out.println(items[i]);
}
}
}

public class Main3 {


public static void main(String[] args) {
ShoppingCart cart = new ShoppingCart(5);

cart.addItem("Shoes");
cart.addItem("Perfume");
cart.addItem("Shirt");
cart.addItem("Chocolate");
cart.addItem("Chips");
cart.displayCart();
}
}

Output:

COLLEGE OF COMPUTER SCIENCE AND INFORMATION SYSTEMS

You might also like