Question: 5 . The below program shows how constructor is used to give initial values to the instance variables defined by the class, or to perform
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 Lab Ex:
Same as Ex: 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 mainString bob
dynaically create object
Food f new Food;
assigning the values
fnuts ;
fflour ;
fveggies ;
fgrains ;
apply discount
whilefnuts
fnuts fnuts ; discount of nuts
fdnuts fnuts ;
whilefflour
fflour fflour ; discount of flour
fdflour fflour ;
whilefveggies
fveggies fveggies ; discount of veggies
fdvegs fveggies ;
whilefgrains
fgrains fgrains ; discount of grains
fdgrains fgrains ;
figure out total discount
ftotal fdnuts fdflour fdvegs fdgrains;
print
System.out.printlnTotl discount from FreshFoods is ftotal dollars.";
Edit Exercise
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
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
