Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Suppose the Get/Set Program example available for download in this weeks lecture was changed slightly. The change was with one line of code and does

Suppose the Get/Set Program example available for download in this weeks lecture was changed slightly. The change was with one line of code and does not throw an error and the program compiles. No lines of code were deleted, but one line has been edited. When the program runs, the output is as follows (user input in BOLD):

This program will determine the number of hours for days, weeks, and months entered.

Please enter a number: 5

There are 0 hours in 5 day(s).

There are 0 hours in 5 week(s).

There are 0 hours in 5 month(s).

What seems to be the problem with the code?

Below is the Main Class Code from this week.

package getsetsample;

/**

* @Course: SDEV 250 ~ Java Programming I

* @Author Name: jwabbs

* @Assignment Name: getsetsample

* @Date: Jul 4, 2017

* @Description: Simple get and set method program to determine hours in day(s),

* week(s) and month(s).

*/

//Imports

import java.util.Scanner;

//Begin Class GetSetSample

public class GetSetSample {

//Begin Main Method

public static void main(String[] args) {

//New Scanner object

Scanner sc = new Scanner(System.in);

//Declarations

int nums;

//Welcome statement

System.out.println("This program will determine the number of hours for days, " + "weeks, and months entered.");

//Ask for input from user

System.out.print(" Please enter a number: ");

nums = sc.nextInt();

//New instance of the subclass and constructor. Send input to subclass

TimeCount myTC = new TimeCount(nums);

/**

Because you have already set the days in the line above, you can now retrieve the final value

*/

System.out.printf("There are %d hours in %d day(s). ", myTC.getDays(), nums);

System.out.printf("There are %d hours in %d week(s). ", myTC.getWeeks(), nums);

System.out.printf("There are %d hours in %d month(s). ", myTC.getMonths(), nums);

/**

Conversely, you can set the values individually as shown below. You can comment out lines 33 - 41 above and uncomment lines 50 - 61 below to see the results. Simply highlight the lines you want to comment (or uncomment) and select Source -> Toggle Comment from the menu at the top of NetBeans. If you decide to try this, you must change the set methods in the subclass to public or you will have errors.

*/

// //New instance of the subclass and constructor.

//TimeCount myTC = new TimeCount();

//

// //Set each value

// // myTC.setDays(nums);

// // myTC.setWeeks(nums);

// // myTC.setMonths(nums);

// //Get the derived values

//

// System.out.printf("There are %d hours in %d day(s). ", myTC.getDays(), nums);

// System.out.printf("There are %d hours in %d week(s). ", myTC.getWeeks(), nums);

// System.out.printf("There are %d hours in %d month(s). ", myTC.getMonths(), nums);

} //End Main Method

}//End Class GetSetSample

Below is the SubClass Code for this week.

package getsetsample;

/** * @Course: SDEV 250 ~ Java Programming I

* @Author Name: jwabb

* @Assignment Name: getsetsample

* @Date: Jul 4, 2017

* @Description: Simple subclass with set and get methods

*/

//Imports

//Begin Class TimeCount

public class TimeCount {

/**

Make some declarations that are private to the subclass here.

Use final to set the default hours. These will not change and

their values do not need to be revealed to the main class; thus the

'private'

Note: Month assumes a 30 day month in this example. Simple subclass with set and get methods

*/

private final int HRS = 24;

private final int WK = 168;

private final int MONTH = 720;

int numD;

int numW;

int numM;

/**

* Constructor - call to set value passed from main

* @param num

*/

public TimeCount(int num) {

setTime(num);

}

/**

* Constructor - Initializes default value of 0

*/

public TimeCount() {

this(0);

}

/**

* Calls to set values for set methods

* @param num

*/

private void setTime(int num) {

setDays(num);

setWeeks(num);

setMonths(num);

} //Set methods***************************************************************

/**

The set methods are used to derive an answer. The sets are void methods, so

they do not return data. They simply act upon what is sent to them and

assign the new value to a variable. These are declared as private because

the initial setup in main does not need to access them directly.

*/

/**

* Determine hours in number of days entered

* setDays is called from setTime above and the value num is passed into the new

* value h. h is multiplied by the final variable hrs to derive the total number

* of hours. The process repeats for other set methods.

* @param h

*/

private void setDays(int h) {

numD = h * HRS;

}

/**

*Determine hours in number of weeks entered

* @param w

*/

private void setWeeks(int w) {

numW = w * WK;

}

/**

* Determine hours in number of months entered

* @param m

*/

private void setMonths(int m) {

numM = m

* MONTH;

}

//Get methods***************************************************************

/**

The get methods are merely a way to call the results after they have been set

& derived within the set methods above. They are not of type void so they

must return a value.

*/

/** * Get hours in days

* @return

*/

public int getDays() {

return numD;

}

/**

* Get hours in weeks

* @return

*/

public int getWeeks() {

return numW; }

/**

* Get hours in months

* @return

*/

public int getMonths() {

return numM;

}

} //End Class TimeCount

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_2

Step: 3

blur-text-image_3

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

Database And Expert Systems Applications 33rd International Conference Dexa 2022 Vienna Austria August 22 24 2022 Proceedings Part 1 Lncs 13426

Authors: Christine Strauss ,Alfredo Cuzzocrea ,Gabriele Kotsis ,A Min Tjoa ,Ismail Khalil

1st Edition

3031124227, 978-3031124228

More Books

Students also viewed these Databases questions

Question

2. Provide recommendations for effective on-the-job training.

Answered: 1 week ago