Answered step by step
Verified Expert Solution
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
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
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started