CS102 Lab session 4

Complete the following exercises pasted below. For each exercise, give an algorithm/java code.



Q1. Execute the following code.
//Q1
       class A{
    private String privateVar = "Private Variable in ClassA";
    protected String protectedVar = "Protected Variable in ClassA";
    public String publicVar = "Public Variable in ClassA";
    
    private void privateMethod() {
        System.out.println("Private Method in ClassA");
    }
    protected void protectedMethod() {
        System.out.println("Protected Method in ClassA");
    }
    public void publicMethod() {
        System.out.println("Public Method in ClassA");
    }

    // Public method to demonstrate access within the same class
    public void accessPrivateInClassA() {
        System.out.println("Accessing private member in ClassA: " + privateVar);
        privateMethod(); // Accessing private method
    }
 
}

public class Test {
    
    public static void main(String[] args) {
        A objA = new A();

        // Access public variable and method (this will work fine)
        System.out.println("Accessing public member: " + objA.publicVar);
        objA.publicMethod();

        // Access protected variable and method (this will work fine as long as it's in the same package)
        System.out.println("Accessing protected member: " + objA.protectedVar);
        objA.protectedMethod();

        // Trying to access private variable (this will cause an error)
        // Uncommenting the following lines will result in a compilation error
        
        // System.out.println("Accessing private member: " + objA.privateVar); // ERROR
        // objA.privateMethod(); // ERROR

        // However, private members can be accessed within the same class through a public method
        System.out.println("\nAccessing private member through a public method:");
        objA.accessPrivateInClassA();
    }
}
            
        }
        


Q2. Class name ChemicalReaction
Attributes: All fields must be private.
• reactionName: String • element1 : Element • element2 : Element Constructors
1. Parameterized constructor ChemicalReaction(String reactionName, Element e1, Element e2)
2. Copy constructor ChemicalReaction(ChemicalReaction other)Creates a deep copy of the reaction (copies elements using their copy constructors).
Methods
• Getters and setters, with the following validation :
o If e1 or e2 is null, replace it with a default Element(Use default constructor)
o If reactionName is null or empty, replace it with “RX”
• Method getInfo(): inputs nothing, returns a string in the following format:
Reaction[, element1 + element2]
For example: Reaction[Combustion, O(16.0g) + H(2.0g)]
Create a class named Element that represents a chemical element with a weight. Attributes: All fields must be private.
• symbol: String • weight: double • DEFAULT_SYMBOL : final String = "X" • MAX_WEIGHT : static final double = 500.0
Constructors
1. Default constructor
o Sets symbol to DEFAULT_SYMBOL o Sets weight to 0.0 2. Parameterized constructor (to initialize symbol and weight)
o Calls the set methods 3. Copy constructor
o Creates a deep copy of the given element (copies the symbol and weight of the other element) o If the other is null, create a default element
Methods
• Getters and setters, with the following validation rules:
o If symbol is null or empty → use DEFAULT_SYMBOL
o If weight < 0 or weight > MAX_WEIGHT → set weight = 0
• Method getInfo(): inputs nothing, returns a string in the following format:
(g). For example, H(16.0g)
//Q2
        public class Test {
    
    public static void main(String[] args) {
        Element e1 = new Element("O", 16);
        Element e2 = new Element("H", 2);
        ChemicalReaction RX1 = new ChemicalReaction("Combustion", e1, e2);
        ChemicalReaction RX2 = new ChemicalReaction(RX1);
        e1.setWeight(20);
        RX2.setElement1(e1);
        System.out.println(RX1.getInfo());
        System.out.println(RX2.getInfo());
        
        
        
    }
}
    

        


Q3. Class Date is given with the following details:
- private int day, month and year - default and parameterized constructor - a equals method that compares dates
- write toString method - write a setter method that sets all three values - write a equals method to test if two dates objects are equal
Class Student is given with the following details:
- public int id - protected String name - private double gpa - protected Date birthDate; - protected Date enrolledDate;
- write a default and parameterized constructor - write toString method - write a setter method that sets the appropriate values
Write a tester program with these details:
- Make a date object D1. set your date of birth. - Make a date object D2. set your date of joining PSU. - Make Student object S1. Use default constructor to initialize. - Set D1 and D2 as Student.birthDate and Student.enrolledDate
- Make another Student object S2. Use parameterized constructor to initialize values. - provide birthDate and enrolledDate for Student S2. - test if birthDate and enrolledDate for S1 are equal or not - test if birthDate and enrolledDate for S2 are equal or not
//Q3
        

        

Solution