Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

5 . The below program shows how constructor is used to give initial values to the instance variables defined by the class, or to perform

5.The below program shows how constructor is used to give initial values to the instance variables defined by the class, or to perform any other startup procedures required to create a fully formed object. /* Unit 3 Lab Ex:5
Same as Ex:4 but will show how constructor is used to give initial values to the instance variables defined by the class, or to perform
any other start up procedures required to create a fully formed object.
*/
// food class
class Food
{
int nuts; //nuts
int flour; // flour
int veggies; // veggies
int grains; // grains
// create variables to hold discount
int dnuts;
int dflour;
int dvegs;
int dgrains;
int total; // total
}
// object of type vehicle
class Discount
{
public static void main(String bob[])
{
// dynaically create object
Food f = new Food();
// assigning the values
f.nuts =20;
f.flour =20;
f.veggies =20;
f.grains =50;
// apply discount
while(f.nuts >=10)
{
f.nuts = f.nuts /10; // discount of nuts
f.dnuts = f.nuts *3;
}
while(f.flour >=20)
{
f.flour = f.flour /20; // discount of flour
f.dflour = f.flour *5;
}
while(f.veggies >=10)
{
f.veggies = f.veggies /10; // discount of veggies
f.dvegs = f.veggies *7;
}
while(f.grains >=50)
{
f.grains = f.grains /50; // discount of grains
f.dgrains = f.grains *10;
}
// figure out total discount
f.total = f.dnuts + f.dflour + f.dvegs + f.dgrains;
// print
System.out.println("Totl discount from FreshFoods is "+ f.total +" dollars.");
}
}
Edit Exercise 4
program to include constructor. Create, compile and run the program. Provide screen capture and code.

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

Database Systems An Application Oriented Approach Complete Version

Authors: Michael Kifer, Arthur Bernstein, Richard Lewis

2nd Edition

0321268458, 978-0321268457

More Books

Students also viewed these Databases questions

Question

=+ (a) Show that A,(i) is trifling.

Answered: 1 week ago

Question

Define Management or What is Management?

Answered: 1 week ago

Question

What do you understand by MBO?

Answered: 1 week ago

Question

5. Structure your speech to make it easy to listen to

Answered: 1 week ago

Question

1. Describe the goals of informative speaking

Answered: 1 week ago