CSE21P8 - Object Oriented Programming - I Lab¶
Java Classes, Objects, Constructors, and Methods¶
Term 161¶
Exp 1. Create a Class named as "GradeBook" that contains a private instance variable named as "CourseName" and three public methods such as (i) setCourseName(), that is used to set the course (ii) getCourseName(), that is used to get course name and returns its value and (iii) displayMessage() that is used to display course name which is called from main class. The course name information read from keyboard in main class. Now write a Java class object based Program to display the entered information.
Exp 2. Write a Java class object based Program to find out the area of a circle and triangle using method overloading concept.
Exp 7. Create a class called Employee that includes three instance variables - a first name (type String), a last name (type String) and a monthly salary (double). Provide a constructor that initializes the three instance variables. Provide a set and a get method for each instance variable. If the monthly salary is not positive, do not set its value. Write a test app named Employee Test that demonstrates class Employee's capabilities. Create two Employee objects and display each object's yearly salary. Then give each Employee a 10% raise and display each Employee's yearly salary again.
Exp 10. Create a class called Date that includes three instance variables - a month (type int), a day (type int) and a year (type int). Provide a constructor that initializes the three instance variables and assumes that the values provided are correct. Provide a set and a get method for each instance variable. Provide a method displayDate that displays the month, day and year separated by forward slashes (/). Write a test app named Date Test that demonstrates class Date's capabilities.
Term 171¶
Exp 1. Create a class named as "GradeBook" that contains a private instance variable named as "CourseName" and three public methods such as
setCourseName(), that is used to set the coursegetCourseName(), that is used to get course name and returns its value anddisplayMessage()that is used to display course name which is called from main class.
The course name information read from keyboard in main class. Now write a Java class object based program to display the entered information.
Exp 4. Create a class called Employee that includes three instance variables - a first name (type String), a last name (type String) and a monthly salary (double). Provide a constructor that initializes the three instance variables. Provide a set and a get method for each instance variable. If the monthly salary is not positive, do not set its value. Write a test app named Employee Test that demonstrates class Employee's capabilities. Create two Employee objects and display each object's yearly salary. Then give each Employee a 10% raise and display each Employee's yearly salary again.
Exp 5. Create a class called Date that includes three instance variables - a month (type int), a day (type int) and a year (type int). Provide a constructor that initializes the three instance variables and assumes that the values provided are correct. Provide a set and a get method for each instance variable. Provide a method displayDate that displays the month, day and year separated by forward slashes (/). Write a test app named Date Test that demonstrates class Date's capabilities.
Exp 10. Write a Java class object based program to find out the area of a circle and triangle using method overloading concept.
Term 191¶
Exp 3. Create a class called Date that includes three instance variables - a month (type int), a day (type int) and a year (type int). Provide a constructor that initializes the three instance variables and assumes that the values provided are correct. Provide a set and a get method for each instance variable. Provide a method displayDate that displays the month, day and year separated by forward slashes (/). Write a test app named Date Test that demonstrates class Date's capabilities.
Exp 7. Drivers are concerned with the mileage their automobiles get. One driver has kept track of several trips by recording the miles driven and gallons used for each tankful. Develop a Java application that will input the miles driven and gallons used (both as integers) for each trip. The program should calculate and display the miles per gallon obtained for each trip and print the combined miles per gallon obtained for all trips up to this point. All averaging calculations should produce floating-point results. Use class Scanner and sentinel-controlled repetition to obtain the data from the user.
Exp 8. Create a class called Invoice that a hardware store might use to represent an invoice for an item sold at the store. An Invoice should include four pieces of information as instance variables - a part number (type String), a part description (type String), a quantity of the item being purchased (type int) and a price per item (double). Your class should have a constructor that initializes the four instance variables. Provide a set and a get method for each instance variable. In addition, provide a method named getInvoiceAmount that calculates the invoice amount (i.e., multiplies the quantity by the price per item), then returns the amount as a double value. If the quantity is not positive, it should be set to 0. If the price per item is not positive, it should be set to 0.0. Write a test app named InvoiceTest that demonstrates class Invoice's capabilities.
Exp 9. Create a class named as "GradeBook" that contains a private instance variable named as "CourseName" and three public methods such as
setCourseName(), that is used to set the coursegetCourseName(), that is used to get course name and returns its value anddisplayMessage()that is used to display course name which is called from main class.
The course name information read from keyboard in main class. Now write a Java class object based program to display the entered information.
Exp 11. Write a Java class object based program to find out the area of a circle and triangle using method overloading concept.
Term 211¶
Exp 07. Create a class called Complex for performing arithmetic with complex numbers. Complex numbers have the form
Write a program to test your class. Use floating-point variables to represent the private data of the class. Provide a constructor that enables an object of this class to be initialized when it's declared. Provide a no-argument constructor with default values in case no initializers are provided. Provide public methods that perform the following operations:
- Add two Complex numbers: The real parts are added together and the imaginary parts are added together.
- Subtract two Complex numbers: The real part of the right operand is subtracted from the real part of the left operand, and the imaginary part of the right operand is subtracted from the imaginary part of the left operand.
- Print Complex numbers in the form
(realPart, imaginaryPart).
Exp 10. Write a program in Java Program to demonstrate the method and constructor overloading.