Question

Write a brief report. You should add to your code a solution to the following user...

Write a brief report.

You should add to your code a solution to the following user story: Make a random path generator that creates a path that goes through all the cities exactly once. For example, a random path might be 4, 2, 1, 3.

Turn in your code, as well as some console output showing your code is working.

0 0
Add a comment Improve this question Transcribed image text
Answer #1
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Scanner;

public class Main {

    public static void main(String[] args) {

        System.out.print("Enter number of cities: ");
        Scanner sc = new Scanner(System.in);

        int n = sc.nextInt();

        ArrayList<Integer> cities = new ArrayList<>();
        for (int x = 1; x <= n; x++) {
            cities.add(x);
        }
        Collections.shuffle(cities);
        System.out.println("Cities: " + Arrays.toString(cities.toArray()));
    }
}

10 2411 System.out.print(Enter number of cities: ); Console terminated Main (5) [lava Applicati Enter number of cities: 8 Cities: [2, 6, 3, 8, 4, 7, 5, 1] Scanner sc new Scanner (System.in) int n = sc.nextInt(); ArrayList<Integer> cities = new ArrayList()(); ram FilesJavayjdkl8.0_73 binjav 12 13 14 15 16 17 18 19 20 21 for (int x = 1; x <= n; x++) { cities.add(x); Collections.shuffle(cities); System.out.println(Cities:Arrays.toString(cities.toArray())) 23 24 25 26 27 28 29

Add a comment
Know the answer?
Add Answer to:
Write a brief report. You should add to your code a solution to the following user...
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
  • Write the Code in C program. Create a random password generator that contains a user-specified number...

    Write the Code in C program. Create a random password generator that contains a user-specified number of characters. Your program should prompt the user for the desired length of the password. Length of password: To create your password, use the random number generator in the stdlib.h C library. To generate random numbers, you will need the srand() and rand() functions. srand(seed) is used to "seed" the random number generator with the given integer, seed. Prompt the user for the seed...

  • Write an informational memo report to your instructor that describes your desired career. In the report,...

    Write an informational memo report to your instructor that describes your desired career. In the report, summarize the information found in the Handbook about the nature of the work, working conditions, necessary qualifications, and the outlook for this career. Also summarize relevant information about typical salary ranges for this occupation. Add an appropriate introductory paragraph that describes the purpose of the report, and conclude with a brief paragraph summarizing what you learned from this investigation. For instance, after this investigation,...

  • In this assignment, you must write a C program to check the validity of a Sudoku solution. You must at least do the foll...

    In this assignment, you must write a C program to check the validity of a Sudoku solution. You must at least do the following: 1- Ask the user to provide a minimum of first two rows of the Sudoku grid. For the rest of the entries, you should use a random number generator. 2- Use appropriate logic to make sure the random number generator generates a distinct set of valid integers! 3- It should be a console-based, yet convenient and...

  • Java code. Need help with either 1. or 2. Nothing too complicated and the code needs...

    Java code. Need help with either 1. or 2. Nothing too complicated and the code needs at least one loop and 3 switch statements! Artificial Intelligence elements in programming. Creating stories with user input. For this assignment, you can do one of two things 1. Create a chat box that allows a conversation to go on as long as the user wants . Welcome the user when they start. . look for key words or other things that could guide...

  • Write a Java application program that plays a number guessing game with the user. In the...

    Write a Java application program that plays a number guessing game with the user. In the starter code that you are given to help you begin the project, you will find the following lines of code: Random generator = args.length == 0 ? new Random() :                    new Random(Integer.parseInt(args[0])); int secret = generator.nextInt(100); You must keep these lines of code in your program. If you delete these lines, or if you change thse lines in any way, then your program...

  • Please write the following code in C++ for Data Structures class. Please follow all the instructions...

    Please write the following code in C++ for Data Structures class. Please follow all the instructions in the picture below. Please write the program with zero warnings. Description: Please write a short program, randomStream, that asks for the name of a file and a number, and produces a files satisfying the following conditions: The file name is the name given by the user (exactly--no added suffixes please) The number of lines in the file is exactly the same as the...

  • Please go to the Practice Exercises (at the end of Chapter 3 in your text on...

    Please go to the Practice Exercises (at the end of Chapter 3 in your text on page 129) and do Exercise E 3.14 which asks you to compute and print the current season of the year depending on the month and day (input as integers). Be sure to review Java boolean operators, multi-way IF-ELSE statements, Switch statements, and String comparison methods before implementing your Java code. The algorithm for this program has been written for you and is included in...

  • Go back to In Class Lab A where you created a Game Menu. Modify your code...

    Go back to In Class Lab A where you created a Game Menu. Modify your code so that the user can input one of the four choices. Use an if/else statement so that depending on the user’s choice, the correct game will play. If the user inputs a 4 (Exit), your code exits without playing any game. For the MATH and MADLIBS choices, use your code from Lab 1. For the CHOOSE YOUR OWN ADVENTURE choice, write the code. Write...

  • You will create your own silly story based on information provided by the user. For example,...

    You will create your own silly story based on information provided by the user. For example, the parts in bold were entered by the user. For ideas see: Mad Libs. My Silly Story name: Frank Zhang whole number: 345 body part: stomach noun: cup floating point number: 1.23 clothing article: hat destination: Colorado goal: degree The resulting story is: Congratulations! Today is your 345 day. You're off to Colorado! You're off and away! You have brains in your stomach, You...

  • write code in C++ or C, run/load and provide output Fast Flight Airlines is looking for a programmer. They are considering your applicatiorn for employment. As a test of your ability, they want yo...

    write code in C++ or C, run/load and provide output Fast Flight Airlines is looking for a programmer. They are considering your applicatiorn for employment. As a test of your ability, they want you to write the following program This program could be used in their operations with some modifications. Fast Flight Airlines is a specialized airline transport company. They will transport smokers as well as non-smokers on their airline. FOR fAST LIGHT aIRLINES NON SMOKING SECTION After you have...

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
ADVERTISEMENT