a. Display Game player magic point maximum (G3, 5 - 7 points) You are designing
ID: 3573437 • Letter: A
Question
a. Display Game player magic point maximum (G3, 5 - 7 points) You are designing a role playing game. In this program display the maximum magic points given the level of the player. Prompt the user for their player level. Then, display the maximum magic points for that level of player. The player's level determines the maximum magic points according to the following table: Level 1: 10 magic points Level 2: 15 magic points Level 3: 20 magic points Level 4: 25 magic points Level 5: 30 magic points Level 6: 35 magic points Here are some test cases: i. Level 1 player 10 ii. Level 2 player 15 iii. Level 6 player 35 ITEC 2140 Final Exam Spring, 2016 Page 3 of 3 b. Count Negative Numbers in Array (G5 - 7 points) In MagicArrayCount use an array of integers to compute and display the number of negative (less than zero) values in the array. a. Create the array of 5 integers. b. Use a loop to prompt the user to enter each of the 5 integers for the array c. Use a loop to count the negative numbers in the array d. Print the number of negative numbers in the array Here are some test cases: i. Array {-1, 0, 1, 2, 3} will print 1 ii. Array {9, -9, -3, 6} will print 2 iii. Array {1, 2, 3, 4, 5} will print 0
Explanation / Answer
Please follow the code and comments for description :
a)
CODE :
import java.util.Scanner; // required imports
public class game { // class
public static void main(String[] args) { // driver method
Scanner sc = new Scanner(System.in); // scanner class to read the data
System.out.println("Welcome to Role Play Game..!"); // message
System.out.print("Please Enter Your Player Level (1 - 6) : "); // prompt
int level = sc.nextInt(); // get the data
int points; // local variables
switch(level) { // switch between the choice
case 1 : // first case
points = 10;
System.out.println("Level 1 player " + points); // message
break;
case 2 : // second case
points = 15;
System.out.println("Level 2 player " + points); // message
break;
case 3 : // third case
points = 20;
System.out.println("Level 3 player " + points); // message
break;
case 4 : // fourth case
points = 25;
System.out.println("Level 4 player " + points); // message
break;
case 5 : // fifth case
points = 30;
System.out.println("Level 5 player " + points); // message
break;
case 6 : // sixth case
points = 35;
System.out.println("Level 6 player " + points); // message
break;
}
}
}
OUTPUT :
Welcome to Role Play Game..!
Please Enter Your Player Level (1 - 6) : 6
Level 6 player 35
Welcome to Role Play Game..!
Please Enter Your Player Level (1 - 6) : 2
Level 2 player 15
b)
CODE :
import java.util.Arrays;
import java.util.Scanner;
public class NewClass {
public static void main(String[] args) { // driver method
int MagicArrayCount[] = new int[5];
Scanner sc = new Scanner(System.in); // scanner class to read the data
for (int i = 0; i < 5; i++) { // iterate and read the data
System.out.print("Enter the Element " + (i + 1) + " : "); // message
MagicArrayCount[i] = sc.nextInt(); // save the data
}
System.out.println(Arrays.toString(MagicArrayCount)); // print to console
int count = 0; // local variables
for (int j = 0; j < 5; j++) { // iterate over the size
if (MagicArrayCount[j] < 0) { // check for the value
count++; // increment the count
}
}
System.out.println("Number of Negative Numbers in the Array are : " + count); // message
}
}
OUTPUT :
Enter the Element 1 : -5
Enter the Element 2 : 8
Enter the Element 3 : 4
Enter the Element 4 : -9
Enter the Element 5 : 6
[-5, 8, 4, -9, 6]
Number of Negative Numbers in the Array are : 2
Hope this is helpful.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.