Question

In: Computer Science

Step 1: Create a new Java project in NetBeans called “Wedding1” Step 2: Use appropriate data...

Step 1:

Create a new Java project in NetBeans called “Wedding1”

Step 2:

Use appropriate data types to store the following information:

  • The names of the bride and groom
  • The total number of guests at the wedding
  • The square footage of the location (must be accurate to 0.1 square feet).
  • The names of each song in the DJ's playlist. You should use an ArrayList of Strings to store this, and the user should be able to enter as many song names as they wish.
  • The number of guests per square foot of the location.

Step 3:

Add static methods to:

  • Prompt the user for the names of the bride and groom
  • Prompt the user for number of guests at the wedding
  • Prompt the user for the square footage of the location
  • Prompt the user for names of each song in the DJ's playlist
  • Calculate and display the number of guests per square foot of the location.

Step 4:

Add functionality to:

  • Save all data to a file called wedding.txt
  • Load and display the contents of this file

Note: please use basic exception handling (i.e. a try block) when attempting to access a file.

Step 5:

Add logic to your main() method that uses all of the methods you’ve created in order to prompt the user for values, store their responses, save those values to a file, and display the contents of the file.

Solutions

Expert Solution

Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. Thanks

// Wedding.java

import java.io.File;

import java.io.FileNotFoundException;

import java.io.PrintWriter;

import java.util.ArrayList;

import java.util.Scanner;

public class Wedding {

      // scanner for reading user input from keyboard

      static Scanner keyboard = new Scanner(System.in);

      // reads and returns the bride's name

      static String getBrideName() {

            System.out.print("Enter bride's name: ");

            String bride = keyboard.nextLine();

            return bride;

      }

      // reads and returns the groom's name

      static String getGroomName() {

            System.out.print("Enter groom's name: ");

            String groom = keyboard.nextLine();

            return groom;

      }

      // reads and returns the number of guests

      static int getNumGuests() {

            System.out.print("Enter number of guests: ");

            int guests = Integer.parseInt(keyboard.nextLine());

            return guests;

      }

      // reads and returns the square footage

      static double getSquareFootage() {

            System.out.print("Enter square footage of the location: ");

            double sqft = Double.parseDouble(keyboard.nextLine());

            return sqft;

      }

      // reads and returns the list of songs in DJ's playlist

      static ArrayList<String> getPlayList() {

            ArrayList<String> playList = new ArrayList<String>();

            System.out.println("Enter songs in DJ's playlist in separate lines. "

                        + "Type 'done' to finish");

            String input = keyboard.nextLine();

            // looping until user enter 'done'

            while (!input.equalsIgnoreCase("done")) {

                  // adding to playlist

                  playList.add(input);

                  // reading next song name

                  input = keyboard.nextLine();

            }

            return playList;

      }

      // calculates and returns the number of guests per square foot using number

      // of guests and sqft

      static double getNumGuestsPerSquareFoot(int guests, double sqft) {

            double guestsPerSqFt = (double) guests / sqft;

            return guestsPerSqFt;

      }

      // writes all data to a file with given name

      static void saveData(String filename, String bride, String groom,

                  int numGuests, double sqft, ArrayList<String> playList,

                  double numGuestsPerSqFt) throws FileNotFoundException {

            // opening file in write mode

            PrintWriter writer = new PrintWriter(new File(filename));

            // writing all data

            writer.println("Couple: " + bride + " & " + groom);

            writer.println("Number of guests: " + numGuests);

            // displaying square footage with 1 digit precision

            writer.printf("Square footage of location: %.1f\n", sqft);

            writer.println("Playlist: ");

            // writing all songs

            for (String song : playList) {

                  writer.println("\t" + song);

            }

            // displaying number of guests per sqft with 2 digits precision

            writer.printf("Number of guests per square footage: %.2f\n",

                        numGuestsPerSqFt);

            // closing file, saving changes

            writer.close();

      }

      // loads a file and display the contents on screen

      static void loadAndPrintData(String filename) throws FileNotFoundException {

            Scanner scanner = new Scanner(new File(filename));

            while (scanner.hasNext()) {

                  System.out.println(scanner.nextLine());

            }

            scanner.close();

      }

      public static void main(String[] args) {

            // reading values in order

            String brideName = getBrideName();

            String groomName = getGroomName();

            int numGuests = getNumGuests();

            double squareFootage = getSquareFootage();

            ArrayList<String> playList = getPlayList();

            // calculating numGuestsPerSquareFoot

            double numGuestsPerSquareFoot = getNumGuestsPerSquareFoot(numGuests,

                        squareFootage);

            // opening try block, saving data to file, and loading from file,

            // displaying contents

            try {

                  saveData("wedding.txt", brideName, groomName, numGuests,

                              squareFootage, playList, numGuestsPerSquareFoot);

                  loadAndPrintData("wedding.txt");

            } catch (FileNotFoundException e) {

                  e.printStackTrace();

            }

      }

}

/*OUTPUT*/

Enter bride's name: Lyla

Enter groom's name: John

Enter number of guests: 50

Enter square footage of the location: 2000

Enter songs in DJ's playlist in separate lines. Type 'done' to finish

Faded

Hello

Coldplay

Shape of You

done

Couple: Lyla & John

Number of guests: 50

Square footage of location: 2000.0

Playlist:

      Faded

      Hello

      Coldplay

      Shape of You

Number of guests per square footage: 0.03


Related Solutions

Step 1: Create a new Java project called Lab5.5. Step 2: Now create a new class...
Step 1: Create a new Java project called Lab5.5. Step 2: Now create a new class called aDLLNode. class aDLLNode { aDLLNode prev;    char data;    aDLLNode next; aDLLNode(char mydata) { // Constructor data = mydata; next = null;    prev = null;    } }; Step 3: In the main() function of the driver class (Lab5.5), instantiate an object of type aDLLNode and print the content of its class public static void main(String[] args) { System.out.println("-----------------------------------------");    System.out.println("--------Create...
Part 1 – Classes and objects Create a new Java project called usernamePart1 in NetBeans. In...
Part 1 – Classes and objects Create a new Java project called usernamePart1 in NetBeans. In my case the project would be called rghanbarPart1. Select the option to create a main method. Create a new class called Vehicle. In the Vehicle class write the code for: • Instance variables that store the vehicle’s make, model, colour, and fuel type • A default constructor, and a second constructor that initialises all the instance variables • Accessor (getters) and mutator (setters) methods...
Java programming language: 1.      Create a new Netbeans project called ArrayLoop. 2.      Declare a fixed length array of...
Java programming language: 1.      Create a new Netbeans project called ArrayLoop. 2.      Declare a fixed length array of integers which can store 8 elements. Do not assign any values. int[] myAddresses = new int[8]; 3.      Create a statement which raises 2 to the power of your loop counter, then subtracts 1, and assigns that value to the corresponding element. For example, when i = 3, 2 to the third power is 8, minus 1 = 7. When i = 4, 2 to the...
Start NetBeans. Create a new project called Lab7. Create a Java main class file using the...
Start NetBeans. Create a new project called Lab7. Create a Java main class file using the class name YourlastnameLab7 with your actual last name. Create a Java class file for a Polygon class. Implement the Polygon class. Add a private instance variable representing the number of sides in the polygon. Add a constructor that takes a single argument and uses it to initialize the number of sides. If the value of the argument is less than three, display an error...
2. Create a new NetBeans project called PS1Gradebook. Your program will simulate the design of a...
2. Create a new NetBeans project called PS1Gradebook. Your program will simulate the design of a student gradebook using a two-dimensional array. It should allow the user to enter the number of students and the number of assignments they wish to enter grades for. This input should be used in defining the two-dimensional array for your program. For example, if I say I want to enter grades for 4 students and 5 assignments, your program should define a 4 X...
Step 1: Create a new Java project named ContainerPartyProject --------- Step 2: Add a ContainerParty class...
Step 1: Create a new Java project named ContainerPartyProject --------- Step 2: Add a ContainerParty class to your project. It should contain:             - The date of the party             - A list of people attending             - A list of containers at the party             - The address of the party (this can be either a String or a Class) --------- Step 3: Add a Container class in your project. It should be an abstract class. Your Container class...
1. Create a new Java project called L2 and a class named L2 2. Create a...
1. Create a new Java project called L2 and a class named L2 2. Create a second class called ArrayExaminer. 3. In the ArrayExaminer class declare the following instance variables: a. String named textFileName b. Array of 20 integers named numArray (Only do the 1st half of the declaration here: int [] numArray; ) c. Integer variable named largest d. Integer value named largestIndex 4. Add the following methods to this class: a. A constructor with one String parameter that...
Create a new Java project using NetBeans, giving it the name L-14. In java please Read...
Create a new Java project using NetBeans, giving it the name L-14. In java please Read and follow the instructions below, placing the code statements needed just after each instruction. Leave the instructions as given in the code. Declare and create (instantiate) an integer array called List1 that holds 10 places that have the values: 1,3,4,5,2,6,8,9,2,7. Declare and create (instantiate) integer arrays List2 and List3 that can hold 10 values. Write a method called toDisplay which will display the contents...
in Java using netbeans create a project and in it a class with a main. We...
in Java using netbeans create a project and in it a class with a main. We will be using the Scanner class to read from the user. At the top of your main class, after the package statement, paste import java.util.Scanner; Part A ☑ In your main method, paste this code. Scanner scan = new Scanner(System.in); System.out.println("What is x?"); int x = scan.nextInt(); System.out.println("What is y?"); int y = scan.nextInt(); System.out.println("What is z?"); int z = scan.nextInt(); System.out.println("What is w?");...
Part A Java netbeans ☑ Create a project and in it a class with a main....
Part A Java netbeans ☑ Create a project and in it a class with a main. We will be using the Scanner class to read from the user. At the top of your main class , after the package statement, paste import java.util.Scanner; As the first line inside your main method, paste Scanner scan = new Scanner(System.in); Remember when you are getting a value from the user, first print a request, then use the Scanner to read the right type...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT