Question

textbook question:A method returning an array of letters with their highest point value in any country...

textbook question:A method returning an array of letters with their highest point value in any country My question: I am trying to make a new method return an array of letters with the highest point value in any country. i have already made one method that finds out if the country has a letter with a value of ten. But I cant figure out how to make one that will print out the letter that is the highest value in each country. Just for some extra help I have included another method I have already written.

Extra clarification for question that professor gave: the array to be returned by the first non-constructor method described is to be an array of `int`, laid out so that the slot indexed with 0 holds the requested value for `'a'`, the slot indexed with 1 holds the requested value for `'b'`, and so on.

Code: ========================================================================================

public class TenScrabble
  
{

  
String[] countries = {"America", "Canada", "Chile", "Mexico", "Germany",
"Norway", "Russia", "Japan", "Sweden", "Iraq"};
  
private char[] letters = {'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o',
'p','q','r','s','t','u','v','w','x','y','z'};
  
  
private int[][] values= new int[10][26]; //Could also use new int[countries.length][letters.length]
  
  
  
public TenScrabble()
{
/*Randomizes the letter values*/
for (int c = 0; c < countries.length; c++)
{
Random randGen = new Random();
for (int l = 0; l < letters.length; l++)
{

values[c][l] = randGen.nextInt(10) + 1;
}
}
}//end of constructor
  
  
  
public char[] highestLetters()
{
char[] highestValueLetter = new char[countries.length];

  
for(int i =0; i< values.length; i++)
{
int max = Integer.MIN_VALUE;//placeholder which sets the max to the smallest possible number
char highestLetter = 'a';
  
for(int j=0; j < values[i].length;j++)
{
if(values[i][j] > max)
{
max = values[i][j];
highestLetter = letters[j];
  
}
  
}
highestValueLetter[i] = highestLetter;
  
}
  

  
  
return highestValueLetter;
  

}//end of highestLetters
  
  

  
  

  


/*Prints out countries that have a letter with a value of 10*/
public void getLetterValue10()
{
int countriesWithValue = 0;//keeps track of the number of countries with value so it can print out none if none show up
  
System.out.println("Countries that have a letter with a value of 10");
for(int i = 0; i < values.length;i++)
{
for(int j =0; j<values[0].length;i++)
{
if(values[i][j] == 10)
{
System.out.println(countries[i]);
countriesWithValue++;
}
break;//breaks out of inner loop
}
}
  
  
/*If no countries have a letter with a value of 10 it prints out message to inform user*/
if(countriesWithValue == 0)
{
System.out.println("No countries have a letter with a value of 10");
}
  
  

}//end of getLetterValue10

0 0
Add a comment Improve this question Transcribed image text
Answer #1

IF YOU HAVE ANY DOUBTS COMMENT BELOW I WILL THERE TO HELP YOU

PLEASE RATE THUMBSUP

ANSWER:

CODE:

The required function is in bold

import java.util.*;

class Main {

String[] countries = {"America", "Canada", "Chile", "Mexico", "Germany",

"Norway", "Russia", "Japan", "Sweden", "Iraq"};

private char[] letters = {'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o',

'p','q','r','s','t','u','v','w','x','y','z'};

private int[][] values= new int[10][26];

public void TenScrabble()

{

for (int c = 0; c < countries.length; c++)

{

Random randGen = new Random();

for (int l = 0; l < letters.length; l++)

{

values[c][l] = randGen.nextInt(10) + 1;

}

}

}

/*Prints out countries that have a letter with a value of ten*/

public void getLetterValue10()

{

int countriesWithValue = 0;

System.out.println("Countries that have a letter with a value of 10");

for(int i = 0; i < values.length;i++)

{

for(int j =0; j < values[i].length; j++)

{

if(values[i][j] == 10)

{

System.out.println(countries[i]);

countriesWithValue++;

}

break;//breaks out of inner loop

}

}

/*If no countries have a letter with a value of 10 it prints out message to inform user*/

if(countriesWithValue == 0)

{

System.out.println("No countries have a letter with a value of 10");

}

}//end of getLetterValue10

public char[] getHighestValueLetters()

{

char[] highestValueLetters = new char[10];

for(int i = 0; i < values.length; i++)

{

char maxLetter = 'a';

int highest = -1;//Let highest value be -1 for the country

for(int j = 0; j < values[i].length; j++)

{

if(values[i][j] > highest)

{

highest = values[i][j];

maxLetter = letters[j];

}

}

highestValueLetters[i] = maxLetter;

}

return highestValueLetters;

}//End of getHighestValueLetters

public static void main(String[] args) {

Main m = new Main();

m.TenScrabble();

m.getLetterValue10();

char[] highestLetters = m.getHighestValueLetters();

System.out.println("Highest letter value for each country:");

for(int i = 0; i < highestLetters.length; i++)

System.out.println(highestLetters[i]);

}

}

RATE THUMBSUP PLEASE

Add a comment
Know the answer?
Add Answer to:
textbook question:A method returning an array of letters with their highest point value in any country...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • I'm writing this class called CharArrayProject_3 that removes repeating elements from an array of chars. However,...

    I'm writing this class called CharArrayProject_3 that removes repeating elements from an array of chars. However, when I run the driver class, it just outputs two sets of dashed lines. What am I getting wrong? Is it the deleteRepeats() method?: public class CharArrayProject_3 { private char[] array; privateint length; privateintnumberOfRepeats; public CharArrayProject_3( char[] arr ) { length = arr.length; array = new char[ length ]; numberOfRepeats = 0; for( int k = 0; k < arr.length; k++ ) { array[k]...

  • To the HighArray class in the highArray.java, add a method called getMax() that returns the value...

    To the HighArray class in the highArray.java, add a method called getMax() that returns the value of the highest key in the array, or -1 if the array is empty. Add some code in main() to exercise this method. You can assume all the keys are positive numbers. // highArray.java class HighArray { private long[] a; private int nElems;    public HighArray(int max)    { a = new long[max];    nElems = 0; } public boolean find(long searchKey) { int...

  • How to I change this code to print the highest value(s) in the array? The wanted...

    How to I change this code to print the highest value(s) in the array? The wanted output (you just need to make it print the winner, ignore what comes before) is included below: -------------------------------------------------------------------------------------------- public static void playGame(int players, int cards) { if (players * cards > 52) { System.out.println("Not enough cards for that many players."); return; } boolean[] deck = new boolean[52]; int[] playerScore = new int[players]; for (int i = 0; i < players; i++) { System.out.println("Player "...

  • . In the method main, prompt the user for a non-negative integer and store it in...

    . In the method main, prompt the user for a non-negative integer and store it in an int variable num (Data validation is not required for the lab). Create an int array whose size equals num+10. Initialize the array with random integers between 0 and 50 (including 0 and excluding 50). Hint: See Topic 4 Decision Structures and File IO slides page 42 for how to generate a random integer between 0 (inclusive) and bound (exclusive) by using bound in...

  • This is the assignment..... Write a class DataSet that stores a number of values of type...

    This is the assignment..... Write a class DataSet that stores a number of values of type double. Provide a constructor public DataSet(int maxNumberOfValues) and a method public void addValue(double value) that add a value provided there is still room. Provide methods to compute the sum, average, maximum and minimum value. ​This is what I have, its suppose to be using arrays also the double smallest = Double.MAX_VALUE; and double largest = Double.MIN_VALUE;​ are there so I don't need to create...

  • need help editing or rewriting java code, I have this program running that creates random numbers...

    need help editing or rewriting java code, I have this program running that creates random numbers and finds min, max, median ect. from a group of numbers,array. I need to use a data class and a constructor to run the code instead of how I have it written right now. this is an example of what i'm being asked for. This is my code: import java.util.Random; import java.util.Scanner; public class RandomArray { // method to find the minimum number in...

  • I need help making this work correctly. I'm trying to do an array but it is...

    I need help making this work correctly. I'm trying to do an array but it is drawing from a safeInput class that I am supposed to use from a previous lab. The safeInput class is located at the bottom of this question I'm stuck and it is not printing the output correctly. The three parts I think I am having most trouble with are in Bold below. Thanks in advance. Here are the parameters: Create a netbeans project called ArrayStuff...

  • JAVA 1.Write a static method named getMaxEven(numbers) which takes an array of positive integers as a...

    JAVA 1.Write a static method named getMaxEven(numbers) which takes an array of positive integers as a parameter. This method calculates and returns the largest even number in the list. If there are no even numbers in the array, the method should return 0. You can assume that the array is not empty. For example: Test Result int[] values = {1, 4, 5, 9}; System.out.println(getMaxEven(values)); 4 System.out.println(getMaxEven(new int[]{1, 3, 5, 9})); 0 public static int --------------------------------------------------------------------------------- 2. Write a static method...

  • Deck of Cards Program I need help printing a flush, which is showing the top 5...

    Deck of Cards Program I need help printing a flush, which is showing the top 5 cards of the same suite. Below is the code I already have that answers other objectives, such as dealing the cards, and finding pairs. Towards the end I have attempted printing a flush, but I cannot figure it out. public class Shuffler {    /**    * The number of consecutive shuffle steps to be performed in each call    * to each sorting...

  • Create a program named IntegerFacts whose Main() method declares an array of 10 integers. Call a...

    Create a program named IntegerFacts whose Main() method declares an array of 10 integers. Call a method named FillArray to interactively fill the array with any number of values up to 10 or until a sentinel value (999) is entered. If an entry is not an integer, reprompt the user. Call a second method named Statistics that accepts out parameters for the highest value in the array, lowest value in the array, sum of the values in the array, and...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
Active Questions
ADVERTISEMENT