Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

public class InLab2A { // Main method public static void testActivity(){ // Test default constructor System.out.println(Testing default constructor Activity()); Activity act1 = new Activity(); System.out.println(Should

image text in transcribedimage text in transcribedimage text in transcribed

public class InLab2A { // Main method public static void testActivity(){ // Test default constructor System.out.println("Testing default constructor Activity()"); Activity act1 = new Activity(); System.out.println("Should say: 1.1.1"); act1.printDate(); // Test filled constructor // Test good input System.out.println("Testing setDate"); System.out.println("Try 8, 8, 1304:"); Activity act2 = new Activity(8,8,1304); System.out.println("Should say: 8.8.1304"); act2.printDate(); // Test bad day System.out.println("Testing setDate"); System.out.println("Try 40, 8, 1304:"); System.out.println("Should print error:"); Activity act3 = new Activity(40,8,1304); System.out.println("Should say: 1.8.1304"); act3.printDate(); // Test bad month System.out.println("Testing setDate"); System.out.println("Try 8, 0, 1304:"); System.out.println("Should print error:"); Activity act4 = new Activity(8,0,1304); System.out.println("Should say: 8.1.1304"); act4.printDate(); // Test bad year System.out.println("Testing setDate"); System.out.println("Try 8, 8, -1304:"); System.out.println("Should print error:"); Activity act5 = new Activity(8,8,-1304); System.out.println("Should say: 8.8.1"); act5.printDate(); // Test date setting System.out.println("Testing setDate"); System.out.println("Try 12, 2, 2017:"); act1.setDate(12, 2, 2017); System.out.println("Should say: 12.2.2017"); act1.printDate(); System.out.println("Try 30, 2, 2017:"); System.out.println("Should print error:"); act1.setDate(30, 2, 2017); System.out.println("Should say: 12.2.2017"); act1.printDate(); System.out.println("Try 5, 6, 2020:"); act1.setDate(5, 6, 2020); System.out.println("Should say: 5.6.2020"); act1.printDate(); System.out.println("Try 31, 4, 2017:"); System.out.println("Should print error:"); act1.setDate(31, 4, 2017); System.out.println("Should say: 5.4.2017"); act1.printDate(); System.out.println("Try 10, 14, 2017:"); System.out.println("Should print error:"); act1.setDate(10, 14, 2017); System.out.println("Should say: 10.4.2017"); act1.printDate(); act1.setDate(10, 4, 2017); // Test Accessors // Test getDay System.out.println("Should say 10: " + act1.getDay()); // Test getMonth System.out.println("Should say 4: " + act1.getMonth()); // Test getYear System.out.println("Should say 2017: " + act1.getYear()); } // Main method public static void testStock(){ System.out.println(" Test Stock "); Stock asset1 = new Stock(); System.out.println("Should print: : 1.1.1 values: 0, 0, 0, 0, 0, 0, 0"); asset1.printStock();

System.out.println("Checking Full Constructor:"); // Check that shot list doesn't work int[] values1 = new int[1]; values1[0] = 1077; System.out.println("Should give error: "); Stock asset2 = new Stock(15,5,2017,"GOOGL",values1); System.out.println("Should print: GOOGL: 15.5.2017 values: 0, 0, 0, 0, 0, 0, 0"); asset2.printStock(); // Check that the full constructor works int[] values2 = new int[7]; for(int i = 0; i For this lab you must make two classes. The first class will be called "Activity" and is used to store dates. We will discuss the basics of setting up this class in Java using Eclipse. 1. Create a new class in you project and call it "Activity" 2. As we wish to use this class in other files ensure that it is public 3. Within the class brackets add three private instance variables (i.e., attributes): (a) private int day: (b) private int month; (c) private int year; 4. It is good practice to make your attributes private. This prevents anyone from changing an object's information willy nilly. Additionally, through the use of en- capsulation techniques we can check that all changes are acceptable. In our case we will have to ensure that the date is always a valid one. so that people can't input more than the number of days in a month or more than the number of months in a year. 5. Our next step is to add a constructor 6. After your attributes write down public Activity(){} this will be our default constructor 7. Within this constructor set all of our instance variables (attributes) to 1 8. Add another constructor, public Activity (int dayIn, int monthIn, int yearIn){} this will be used to initialize our instance variables (attributes). 9. Following this set the year then the month then the day to the input date. Ensure that you check that the date entered is valid. 10. Now add 3 methods to get the value of each of our attributes. These methods are called accessors as they access our attributes. (a) public int get Day(){} (b) public int get Month(){} (c) public int get Year(){} 11. Fill in these methods to return their matching attribute value. 12. Now we will add a mutator method that sets the date to something else. 13. Write a method public void setDate(int dayIn, int monthIn, int yearIn){} 14. Update the attributes with these values only if it is a valid date. Otherwise print an error statement using: System.out.println() 15. Last, add a method to print the contents of our object 16. Call the method public void print Date(){} and have it print day.month.year" 17. Now run the project 4 Expanding on Our Class Having a class that stores the date can be very useful, but we can add more to our class! You will now create a class "Stock. That will pass the test code testStock(). 1. Go to your main method in "InLab2" 2. Add the line testStock(); to the method. this will now run the test code for our new class. With that done lets get to building our stock class. It is a class that describes the daily value of a stock for each of the hours the market is open. It will have all of the attributes from our previous class (day, month, year) and two new ones. These new attributes will be the stock name and a list of 7 values of the stock through the day. 1. First create a class and name it "Stock" 2. Copy over the contents of "Activity" to "Stock and remove the constructors 3. With the other attributes add two more. (a) private String name; (b) private int[] values = new int[7]; 4. Add a new default constructor public Stock(){} 5. As before set the date to ls, but then set the name to the empty String", and all of the elements of values to 0. 6. Now add a constructor that will initialize all the attributes 7. Add the constructor public Stock(int dayIn, int monthIn, int yearIn, String nameln, int ( valIn){} 8. Check that the date is valid, and that valln is of length 7 with no negative number. We cant have a stock that is worth less than nothing. 9. If the arguments are all valid create set all the attributes accordingly. Otherwise set all attributes to the default and print an error message. 10. Now we must alul aruteurs ur uur new attribulus 11. First create the method public String getName() { } and have it return the name 12. Then create public int getValue(int ind){} a 13. This method must return the value at int or print an error statement and return -1 if an out of bounds index is used. 14. Now we will add mutators for our new attributes (a) public void setName(String nameIn){} changes the name atribute (b) public void setValue(int val, int ind){} changes the value at index ind and prints an error statement if val

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Climate And Environmental Database Systems

Authors: Michael Lautenschlager ,Manfred Reinke

1st Edition

1461368332, 978-1461368335

More Books

Students also viewed these Databases questions

Question

What potential obstacles stand in my way?

Answered: 1 week ago

Question

Draw and explain the operation of LVDT for pressure measurement

Answered: 1 week ago

Question

3. Define the attributions we use to explain behavior

Answered: 1 week ago