SLC S22 Week2 || The Object Approach In JAVA

fombae-learning-cover.png

Greetings Steemit friends

Section A

    1. What is a class in Java?

(A) A blueprint for creating objects.

    1. What does the new keyword do in Java?

(B) Creates a new object of a class.

    1. Which of the following statements is true about attributes in a class?

(C) Attributes store data for an object.

    1. What is the purpose of a constructor in a class?

(B) To initialize the fields of a class.



Section B

Write a program that demonstrates the creation and use of a class by including attributes, methods, and object instantiation.

I will start by declaring the Laptop class, which we learned during the last session. Next, I have the attribute, which will be used to describe the state of the object (Laptop). Next, we create the constructor, which will take two parameters brand and price.

We used the System.out.printIn() method to print the brand and price in the displayDetails() method.

image.png
image.png

In the Main Method, we can now create objects specifying the value for the brand and price attributes. We can then use any of the objects to call the displayDetails() method.

image.png

Write a program that demonstrates the creation of a Movie class

This task is similar to the previous one, but we will be creating a class Movie and will demonstrate working with multiple instances. Just like the previous task, we create our class Movie. have the attributes to describe the object state and the constructor, which will be used to create a new Movie object.

The displayInfo() method makes use of the System.out.printIn() method to print the title and rating.

image.png
image.png

In the main method, we have to create an array that will hold more than one object. Then we can populate the array by making use of the constructor passing the title and rating.

To display the content of an array, you need to iterate through the array. That reason is to use a for-each loop to iterate and display details for each element of the array.

image.png

Write a program that demonstrates adding methods with calculations by creating a Product class.

Here is another similar scenario, which is the same step in creating the class Product. Assign the attributes and initialize the attributes by creating a constructor.

We have the calculateTotalPrice() method to compute the total price by multiplying the price by the quantity, The Next method is the displayProduct () method for printing out the details, making use of the calculateTotalPrice() method to get the total price.

image.png
image.png

The main method will be similar to the task of creating a Laptop class. You should have the expected outcome. In my case, I decided to work with an array of products.

So I have created an array to hold more than one Product object. Now I can populate the array and display using a loop to iterate through the elements of the array.

image.png

Write a program that manages student records by creating a Student class.

Just like the previous task, create the class Student, assign the attributes, and initialize the attributes by creating a constructor.

We create the calculateAverage() method to sum up the marks and divide by the number of marks. Next is the displayDetails() method to display the student details, calling the calculatedAverage() method to calculate and print the average.

image.png
image.png
image.png

The main method is the same. Create the array for Student objects, Populatea, and make use of a loop statement to integrate through the elements of the array.

image.png

Write a program to simulate a simple Library Management System

Here is the trick task of the week, but I was able to get something in the end. With the guide, we have to create three classes starting with the main class ManagementSystem, and later the Book and Library class. Just like the previous task, I assigned the attributes and created the constructor.

In the Book class, we have three methods. The borrowBook() to check if a book is available, returnBook() to confirmed the availability of a book and displayDetails() to display the details

Next class Library to manage the collections of books making use of the ArrayList. Create the constructor and ArrayList<>() to have dynamical book objects created. Next, we have the addBook() method to add a new book object, displayAllBooks() to iterate and display elements of the array, searchByTitle() to search for books using the title, borrowBook() method to get a book by its id and finally the returnBook() method to check a book by its id and mark as a return.

Main method: Here use the Library object to add three books to the store. For a user to be able to take actions, I use a while loop to display the options first which can then be triggered. The Scanner is to help read the user choice which will be passed in the switch statement.



Cheers
Thanks for dropping by
@fombae

Sort:  
Loading...

Coin Marketplace

STEEM 0.24
TRX 0.27
JST 0.040
BTC 96938.01
ETH 3456.54
SBD 1.55