CS102 Lab session 3

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



Q1. Write a class person with the following details:
-Add two attributes/variables private int age and private String name.
-Write two constructors with and without parameters
-Write setter and getter methods
-Write a toString() method that returns a string

Write a test program with these details
- Make a object P1 of type Person using default constructor
- Set values for P1
- Make a object P2 of type Person using parameterized constructor
- Set values for P2
- call toString method to display the values
//Q1
        public class Person {
        
        
        }
        public class Test {
        
        
        }        
        


Q2. Write a class person with the following details:
-Add two attributes/variables private int age and private String name.
-Write two constructors with and without parameters
-Write a setter method that takes two parameters age and name.
-Write a getter method for age
-Write a getter method for name
-Write a toString() method that returns a string
-Write a method calculateBirthYear(); this returns the year the Person was born.

Write a test program with these details
- Make a object P1 of type Person using default constructor
- Set values for P1
- Make a object P2 of type Person using parameterized constructor
- Set values for P2
- Make a object P3
- Ask user to provide values for P3
- call toString method to display the values
- call calculateBirthYear() to find the birth year for all objects
//Q2
        public class Person {
        
        
        }
        public class Test {
        
        
        }        
        


Q3. Write a class Robot with the following details:
-Add two attributes/variables
private int charge
static int count
-Write two constructors with and without parameters.
set the values for charge.
increment count.
-Write a copy-constructor
it copies all variables
-Write a setter method that sets charge.
charge value must be 0<=charge<=100; otherwise throw exception
-Write a getter method for charge
-Write a toString() method that returns a string

Write a test program with these details - Make a object r1 of type Robot using default constructor
- Set values for r1
- Make a object r2 of type Robot using parameterized constructor
- Set values for r2
- Make a object r3
- Ask user to provide values for r3. try different values to validate input
- call toString method to display the values
- print count values for each object
//Q3
        public class Robot {
        
        
        }
        public class Test {
        
        
        } 

        

Solution