0% found this document useful (0 votes)
19 views5 pages

Java Homework: OOP Exercises

The document describes programming exercises to create and test classes. 1. The Person class is modified to be in the myutil package and made public. To access it from outside the package, the class must be imported. 2. A test program is written outside the myutil package to test constructing Person objects and calling its methods. 3. The MealCard class is designed to track student food purchases with a balance that is deducted from when items are purchased. It is placed in the myutil package. 4. A test program outside myutil creates MealCard objects to verify the class methods work correctly.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views5 pages

Java Homework: OOP Exercises

The document describes programming exercises to create and test classes. 1. The Person class is modified to be in the myutil package and made public. To access it from outside the package, the class must be imported. 2. A test program is written outside the myutil package to test constructing Person objects and calling its methods. 3. The MealCard class is designed to track student food purchases with a balance that is deducted from when items are purchased. It is placed in the myutil package. 4. A test program outside myutil creates MealCard objects to verify the class methods work correctly.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

PMSCS OOP 602

HW Chap 07
Name: Md. Jeyson Jaman Sawan
Roll: CSE202101001
Level 1 Programming Exercises★

13. Modify the following class to make it a part of the package named myutil. In addition to
adjusting the source file, what are the steps you need to take so that the class becomes
usable/accessible from other classes that are outside of this myutil package?
class Person {
private String name;
public Person( ) {
name = "Unknown";
}
public String getName() {
return name;
}
public void setName(String name) {
[Link] = name;
}
}

Solution:

Steps:

1. First we have to make the Person class public.


2. To access the Person class from outside of the myutil package we need to import the Person
class from myutil package.
14. Write a program that tests the Person class defined in Exercise 13. Place this test program
outside of the myutil package and make sure you can call the constructor and the methods of
the Person class correctly.
Solution:
Level 2 Programming Exercises ★★
15. Design a class that keeps track of a student’s food purchases at the campus cafeteria. A meal card is
assigned to an individual student. When a meal card is first issued, the balance is set to the number of
points. If the student does not specify the number of points, then the initial balance is set to 100 points.
Points assigned to each food item are a whole number. A student can purchase additional points at any
time during a semester. Every time food items are bought, points are deducted from the balance. If the
balance becomes negative, the purchase of food items is not allowed. There is obviously more than one
way to implement the MealCard class. Any design that supports the key functionalities is acceptable. Put
this class in the myutil package.

Solution:
Level 2 Programming Exercises ★★
16. Write a program that tests the meal card class defined in Exercise 15. Define this test program outside
the myutil package. Create one or two meal card objects in the test program and verify that all methods
defined in the meal card class operate correctly.

Solution:

Common questions

Powered by AI

Having default constructor values in a class, such as an initial balance in a meal card system, offers several implications. It simplifies object instantiation by providing an initial state, reducing the chances of objects being initialized with invalid or zero-state values. This default setup can enhance usability and ensure that objects function predictably out-of-the-box. However, it also requires careful consideration if the default value is suitable in most contexts and whether it could lead to misleading outcomes if not correctly adjusted when exceptions arise .

To create a test program for a package-based class managing meal card objects, follow these steps: 1) Ensure the test program imports the necessary classes from the package using 'import myutil.MealCard;'. 2) Within the test program, instantiate one or more objects of the MealCard class. 3) Perform operations by calling various methods on these objects to verify functionality, such as deducting points or adding points, ensuring the program handles all defined scenarios correctly. This process verifies method functionality outside the package context .

Designing a class that allows students to purchase additional meal card points presents several challenges: ensuring accurate balance updates, preventing unauthorized access/modification, and maintaining data integrity across transactions. To address these, implement encapsulation with private balance fields and methods to securely update balances. Use input validation to verify point purchases fall within acceptable parameters. Additionally, synchronization mechanisms could be necessary for concurrent environments to avoid race conditions if multiple threads could access the meal card object simultaneously .

To prevent negative balances in a meal card class, implement a validation mechanism within the method responsible for processing purchases. Before deducting points for any purchase, the method should check if the balance is sufficient to cover the cost. If not, the transaction should be halted, and optionally, an error message or exception should be thrown to notify the user. This ensures the integrity of the balance and prevents any unauthorized transactions when points are insufficient .

To include an existing class in a package and ensure it is accessible from outside the package, you need to modify the class and take several steps: 1) Declare the class as public to make it accessible outside its package. 2) Add a package declaration at the top of the file, specifying the package name, such as 'package myutil;'. 3) In any external class that needs to use the class, import it using the full package name, like 'import myutil.Person;'. These steps ensure the class is part of the specified package and can be utilized by other classes outside that package .

Modularization within a Java package can significantly enhance project maintainability and development efficiency by isolating specific functionalities into cohesive units. This structure allows developers to focus on individual components independently, making the codebase easier to understand and debug. It facilitates reuse across different parts of an application or various projects, enhances collaboration by allowing parallel development, and supports an agile response to change by making refactoring localized without widespread impact on the system .

Importing a class from a package in a Java program impacts the program's structure by allowing external classes to use that specific class. It reduces code duplication, enabling reusability of existing components. This modularity supports maintainability and scalability as updates to imported classes automatically reflect across applications using them. Functionality-wise, it allows for cleaner straight-line code, improving readability and organization by using specific classes without fully qualified names repeatedly throughout the code base .

The design for a class managing a student's food purchases should include the following requirements: 1) An initial balance set at a default number of points (100 points if unspecified). 2) A method to deduct points corresponding to the cost of food items purchased. 3) Ability to handle the purchase of additional points at any time. 4) Prevent food purchases if the balance cannot cover the cost. The class should be flexible to support key functionalities efficiently, structured in a way that encapsulates these behaviors and data within the package 'myutil' .

Declaring a class as public is crucial when making it part of a Java package to ensure it is accessible from outside the package. Without the public modifier, the class has package-private visibility, meaning it can only be accessed by other classes within the same package. Making a class public extends its visibility to other packages, allowing it to be utilized in a broader range of programs and modules .

To test a class placed in a package from an external class, you need to follow these steps: First, import the class from its package using the correct syntax, e.g., 'import myutil.Person;'. Then, create a test program in a different package or the default package that instantiates objects of the class. Within this test program, call and test each method of the class to verify they work as expected. This will confirm the class is fully functional and accessible as anticipated .

You might also like