Say that you are writing a computer game that involves a hero who encounters var
ID: 3804684 • Letter: S
Question
Say that you are writing a computer game that involves a hero who encounters various monsters. Monsters have several characteristics: hit points, strength, age, and name (the class Monster.java is supplied). The number of hit points a monster has is how far it is from death. A monster with a large number of hit points is hard to kill. The hero also has hit points. When the hit points of the hero reach zero, the game is over. The strength of a monster affects how many hit points the hero looses when the monster hits the hero. The age and name of the monster do not affect the outcome of battles between the monster and a hero. Can you make the necessary changes to the Monster class (supplied with this lab) and write a tester (client) class with main method that will present the hero with monsters in increasing order of difficulty. Hints: Monster needs a compareTo() method. Save your program in the MonsterClient.java file.
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/**
* Say that you are writing a computer game that involves a hero who encounters
* various monsters. Monsters have several characteristics: hit points, strength,
* age, and name. The number of hit points a monster has is how far it is from
* death. A monster with a large number of hit points is hard to kill. The hero
* also has hit points.
* When the hit points of the hero reach zero, the game is over. The strength of
* a monster affects how many hit points the hero looses when the monster hits
* the hero. The age and name of the monster do not affect the outcome of battles
* between the monster and a hero.
*
*
Can you make the necessary changes to the Monster class and write a tester
* class with main method that will present the hero with monsters in
* increasing order of difficulty.
* Hints: Monster needs a compareTo() method.
*/
public class Monster {
private int hitPoints, strength, age;
private String name;
public Monster( int hp, int str, int age, String nm )
{
hitPoints = hp; strength = str; this.age = age; name = nm;
}
public int getHitPoints() { return hitPoints; }
public int getStrength() { return strength; }
public int getAge() { return age; }
public String getName() { return name; }
@Override
public String toString()
{
return "HP: " + getHitPoints() + " Str: " + getStrength() + " " + getName();
}
}
Explanation / Answer
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#include<stdio.h>
int main()
temp=pr[i];
pr[i]=pr[pos];
pr[pos]=temp;
temp=bt[i];
bt[i]=bt[pos];
bt[pos]=temp;
temp=p[i];
p[i]=p[pos];
p[pos]=temp;
}
wt[0]=0; //waiting time for initial method is zero
//calculate waiting time
for(i=1;i<n;i++)
avg_wt=total/n; //average waiting time
total=0;
printf(" Process Burst Time Waiting Time Turnaround Time");
for(i=0;i<n;i++)
turnaround
total+=tat[i];
printf(" P[%d] %d %d %d",p[i],bt[i],wt[i],tat[i]);
}
avg_tat=total/n; //average turnaround
printf(" Average Waiting Time=%d",avg_wt);
printf(" Average Turnaround Time=%d ",avg_tat);
return 0;
}
C++ Program
#include<iostream>
using namespace std;
int main()
{
int bt[20],p[20],wt[20],tat[20],pr[20],i,j,n,total=0,pos,temp,avg_wt,avg_tat;
cout<<"Enter Total variety of Process:";
cin>>n;
cout<<" Enter Burst Time and Priority ";
for(i=0;i<n;i++)
{
cout<<" P["<<i+1<<"] ";
cout<<"Burst Time:";
cin>>bt[i];
cout<<"Priority:";
cin>>pr[i];
p[i]=i+1; //contains method variety
}
//sorting burst time, priority and method variety in ascending order mistreatment choice type
for(i=0;i<n;i++)
temp=pr[i];
pr[i]=pr[pos];
pr[pos]=temp;
temp=bt[i];
bt[i]=bt[pos];
bt[pos]=temp;
temp=p[i];
p[i]=p[pos];
p[pos]=temp;
}
wt[0]=0; //waiting time for initial method is zero
//calculate waiting time
for(i=1;i<n;i++)
avg_wt=total/n; //average waiting time
total=0;
cout<<" Process Burst Time Waiting Time Turnaround Time";
for(i=0;i<n;i++)
turnaround
total+=tat[i];
cout<<" P["<<p[i]<<"] "<<bt[i]<<" "<<wt[i]<<" "<<tat[i];
}
avg_tat=total/n; //average turnaround
cout<<" Average Waiting Time="<<avg_wt;
cout<<" Average Turnaround Time="<<avg_tat;
return 0;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#include<iostream>
using namespace std;
int main()
{
int bt[20],p[20],wt[20],tat[20],pr[20],i,j,n,total=0,pos,temp,avg_wt,avg_tat;
cout<<"Enter Total variety of Process:";
cin>>n;
cout<<" Enter Burst Time and Priority ";
for(i=0;i<n;i++)
{
cout<<" P["<<i+1<<"] ";
cout<<"Burst Time:";
cin>>bt[i];
cout<<"Priority:";
cin>>pr[i];
p[i]=i+1; //contains method variety
}
//sorting burst time, priority and method variety in ascending order mistreatment choice type
for(i=0;i<n;i++)
temp=pr[i];
pr[i]=pr[pos];
pr[pos]=temp;
temp=bt[i];
bt[i]=bt[pos];
bt[pos]=temp;
temp=p[i];
p[i]=p[pos];
p[pos]=temp;
}
wt[0]=0; //waiting time for initial method is zero
//calculate waiting time
for(i=1;i<n;i++)
avg_wt=total/n; //average waiting time
total=0;
cout<<" Process Burst Time Waiting Time Turnaround Time";
for(i=0;i<n;i++)
turnaround
total+=tat[i];
cout<<" P["<<p[i]<<"] "<<bt[i]<<" "<<wt[i]<<" "<<tat[i];
}
avg_tat=total/n; //average turnaround
cout<<" Average Waiting Time="<<avg_wt;
cout<<" Average Turnaround Time="<<avg_tat;
return 0;
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#include<stdio.h>
int main()
temp=pr[i];
pr[i]=pr[pos];
pr[pos]=temp;
temp=bt[i];
bt[i]=bt[pos];
bt[pos]=temp;
temp=p[i];
p[i]=p[pos];
p[pos]=temp;
}
wt[0]=0; //waiting time for initial method is zero
//calculate waiting time
for(i=1;i<n;i++)
avg_wt=total/n; //average waiting time
total=0;
printf(" Process Burst Time Waiting Time Turnaround Time");
for(i=0;i<n;i++)
turnaround
total+=tat[i];
printf(" P[%d] %d %d %d",p[i],bt[i],wt[i],tat[i]);
}
avg_tat=total/n; //average turnaround
printf(" Average Waiting Time=%d",avg_wt);
printf(" Average Turnaround Time=%d ",avg_tat);
return 0;
}
C++ Program
#include<iostream>
using namespace std;
int main()
{
int bt[20],p[20],wt[20],tat[20],pr[20],i,j,n,total=0,pos,temp,avg_wt,avg_tat;
cout<<"Enter Total variety of Process:";
cin>>n;
cout<<" Enter Burst Time and Priority ";
for(i=0;i<n;i++)
{
cout<<" P["<<i+1<<"] ";
cout<<"Burst Time:";
cin>>bt[i];
cout<<"Priority:";
cin>>pr[i];
p[i]=i+1; //contains method variety
}
//sorting burst time, priority and method variety in ascending order mistreatment choice type
for(i=0;i<n;i++)
temp=pr[i];
pr[i]=pr[pos];
pr[pos]=temp;
temp=bt[i];
bt[i]=bt[pos];
bt[pos]=temp;
temp=p[i];
p[i]=p[pos];
p[pos]=temp;
}
wt[0]=0; //waiting time for initial method is zero
//calculate waiting time
for(i=1;i<n;i++)
avg_wt=total/n; //average waiting time
total=0;
cout<<" Process Burst Time Waiting Time Turnaround Time";
for(i=0;i<n;i++)
turnaround
total+=tat[i];
cout<<" P["<<p[i]<<"] "<<bt[i]<<" "<<wt[i]<<" "<<tat[i];
}
avg_tat=total/n; //average turnaround
cout<<" Average Waiting Time="<<avg_wt;
cout<<" Average Turnaround Time="<<avg_tat;
return 0;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#include<iostream>
using namespace std;
int main()
{
int bt[20],p[20],wt[20],tat[20],pr[20],i,j,n,total=0,pos,temp,avg_wt,avg_tat;
cout<<"Enter Total variety of Process:";
cin>>n;
cout<<" Enter Burst Time and Priority ";
for(i=0;i<n;i++)
{
cout<<" P["<<i+1<<"] ";
cout<<"Burst Time:";
cin>>bt[i];
cout<<"Priority:";
cin>>pr[i];
p[i]=i+1; //contains method variety
}
//sorting burst time, priority and method variety in ascending order mistreatment choice type
for(i=0;i<n;i++)
temp=pr[i];
pr[i]=pr[pos];
pr[pos]=temp;
temp=bt[i];
bt[i]=bt[pos];
bt[pos]=temp;
temp=p[i];
p[i]=p[pos];
p[pos]=temp;
}
wt[0]=0; //waiting time for initial method is zero
//calculate waiting time
for(i=1;i<n;i++)
avg_wt=total/n; //average waiting time
total=0;
cout<<" Process Burst Time Waiting Time Turnaround Time";
for(i=0;i<n;i++)
turnaround
total+=tat[i];
cout<<" P["<<p[i]<<"] "<<bt[i]<<" "<<wt[i]<<" "<<tat[i];
}
avg_tat=total/n; //average turnaround
cout<<" Average Waiting Time="<<avg_wt;
cout<<" Average Turnaround Time="<<avg_tat;
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.