Given the following 2 class diagrams. You are to write the method header (includ
ID: 3905672 • Letter: G
Question
Given the following 2 class diagrams. You are to write the method header (include opening and closing brackets and a return statement if one is required) for each method as well as the call statement that would be used from another class to call that method. Object and variable declarations are given below.
1
2
3
4
5
Fruit
+ task1():void
+ task2(int pineapple, double plum):void
+ task3():double (use plum)
+ task4(String cherry):int (use apple)
+ task5(int apple, String strawberry):double (use banana)
6
7
8
9
10
Vegetable
- mushroom:String
+ getMushroom():String
+ setMushroom(String mushroom):void
+ task6():void
+ task7(int potato, double onion):int (use carrot)
+ task8(int carrot):int (use potato)
Object and Variable declarations:
Fruit fruit = new Fruit();
Vegetable veg = new Vegetable();
int apple = 0;
double banana = 0;
String cherry = null;
double plum = 0;
int pineapple = 0;
String strawberry = null;
Fruit
1. Method Header
1. Call
2. Method Header
2. Call
3. Method Header
3. Call
4. Method Header
4. Call
5. Method Header
5. Call
Vegetable
6. Method Header
6. Call
7. Method Header
7. Call
8. Method Header
8. Call
9. Method Header
9. Call
10. Method Header
10. Call
1
2
3
4
5
Fruit
+ task1():void
+ task2(int pineapple, double plum):void
+ task3():double (use plum)
+ task4(String cherry):int (use apple)
+ task5(int apple, String strawberry):double (use banana)
Explanation / Answer
Fruit
1. Method Header:
void task1()
{
}
1. Call
fruit.task1();
2. Method Header
void task2(int pineapple,double plum)
{
}
2. Call
fruit.task2(4,2.0);
3. Method Header
double task3()
{
return 4.0;
}
3. Call
plum=fruit.task3();
4. Method Header
int task4(String cherry)
{
return 1;
}
4. Call
apple=fruit.task4(cherry);
5. Method Header
double task5(int apple, String strawberry)
{
return 5.0;
}
5. Call
banana=fruit.task5(apple,banana);
Vegetable
6. Method Header
public String getMushroom()
{
return mushroom;
}
6. Call
veg.getMushroom();
7. Method Header
public void setMushroom(String mushroom)
{
this.mushroom=mushroom;
}
7. Call
veg.setMushroom("msh");
8. Method Header
void task6()
{
}
8. Call
veg.task6();
9. Method Header
int task7(int potato, double onion)
{
return 8;
}
9. Call
carrot=task7(1,2);
10. Method Header
int task8(int carrot)
{
return 10;
}
10. Call
potato=task8(carrot)
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.