Question

In: Computer Science

XML and JAVA Write a Java program that meets these requirements. It is important you follow...

XML and JAVA

Write a Java program that meets these requirements. It is important you follow these requirements closely.

• Create a NetBeans project named LastnameAssign1. Change Lastname to your last name. For example, my project would be named NicholsonAssign1.

• In the Java file, print a welcome message that includes your full name.

• The program should prompt for an XML filename to write to o The filename entered must end with .xml and have at least one letter before the period. If it does not, print a warning and prompt again. Keep nagging the user until a proper file name is entered.

• Prompt the user for 3 integers.

o The three integers represent red, blue, and green channel values for a color. o Process the numbers according to the instructions below

o Once you have handled the 3 input values, prompt the user again o Keep prompting the user for 3 integers until the user enters the single word DONE

o Reading input should not stop for any reason except for when the user enters DONE.

o The user will not mix DONE and the numbers. The user will either enter 3 integers OR the word DONE

• Once the user is done, open the filename entered in the first step and output the data.

o If no numbers were entered, then do not open the file for writing. Simply print a message that no data was entered.

o When 1 or more colors are entered, write out the XML data for each color. See below for the format.

o Sometimes users may enter integer values that are larger than 255 or less than 0. Values like this will need to be “clipped”, that is, converted to good values. For example, if the user enters 300 for red, then the actual red channel value will be 255. If the user entered -10 for the blue channel, then the actual blue channel value will be 0.

You should follow the sample below and your program should exhibit the same behavior. For full credit, your generated XML files must pass validation with NetBeans Check XML.

GENERAL REQUIREMENTS

All Java-related assignments will have the requirements below. While you may find them limiting, their purpose is to help you adopt good programming habits.

1. You should not have one, ginormous main() function. Your program should be modular, that is, define several (more than 2) meaningful functions in addition to main().

2. All functions should have a comment describing their purpose.

3. You may not use the System.exit() function   
4. You must include a comment at the top of your .java file in this format:

/*******************

John Nicholson

CSCI 3020 Section 04

Fall 2019

Assignment 1
This program does ...

*******************/

You should obviously change the name to your name, the section to your section, and write a real description of the program. If this comment is missing, you will automatically lose 50% of the points on the assignment. Your assignment will not be regraded if you do not put in the correct comment.
XML

Solutions

Expert Solution

Code

import java.util.List;

import java.util.Scanner;

import java.util.ArrayList;

import java.io.File;

import java.io.StringWriter;

import javax.xml.bind.JAXBContext;

import javax.xml.bind.JAXBException;

import javax.xml.bind.Marshaller;

public class ProgAssign1

{

    static private class RGB

    {

        public int Red;

        public int Green;

        public int Blue;

        //Correct the user input if required.

        public int RectifyInput(int number){

            if(number < 0)

                return 0;

            if(number > 255)

                return 255;

            

            return number;

        }

    }

    private static String _fileName;

    private static List<RGB> _userInputs;

    private static Scanner _input;

    //Function to validate the user entered file name.

    public static Boolean ValidateFileName(String fileName) {

        if(fileName == null)

            return false;

        var nameParts = fileName.split("\\.");

        

        if(nameParts.length < 2)

            return false;

        if(!nameParts[nameParts.length - 1].equals("xml"))

            return false;

        if(!nameParts[nameParts.length - 2].matches("[a-zA-Z0-9]+"))

            return false;

        _fileName = fileName;

        return true;

    }

    //Function to get file name from user.

    public static void GetFileName() {

        while(true)

        {

            System.out.print("Enter a file name ending with .xml: ");

            String name = _input.nextLine();

            if(ValidateFileName(name))

                break;

            else

                System.out.println("Invalid file name");

        }

    }

    //Function to get all the user inputs.

    public static void GetUserInput() {

        _userInputs = new ArrayList<RGB>();

        String r, g, b;

        while(true)

        {

            System.out.println("Enter 3 integers");

            r = _input.nextLine();

            if(r.equals("DONE")) break;

            g = _input.nextLine();

            if(g.equals("DONE")) break;

            b = _input.nextLine();

            if(b.equals("DONE")) break;

            var color = new RGB();

            color.Red = color.RectifyInput(Integer.parseInt(r));

            color.Green = color.RectifyInput(Integer.parseInt(g));

            color.Blue = color.RectifyInput(Integer.parseInt(b));

            _userInputs.add(color);  

        }

    }

    //Write user input to File

    private static void WriteToFile(){

        if(_userInputs.size() == 0)

        {

            System.out.println("No data was entered!");

            return;

        }

        JAXBContext jaxbContext = JAXBContext.newInstance(RGB.class);

        Marshaller jaxbMarshaller = jaxbContext.createMarshaller();

        jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);

        File file = new File(_fileName);

        jaxbMarshaller.marshal(_userInputs, file);

    }

    public static void main(String[] args) {

        System.out.println("Your Name");

        _input = new Scanner(System.in);

        GetFileName();

        GetUserInput();

        WriteToFile();

        System.out.println("Done.");

        _input.close();

    }

}


Related Solutions

1. Write a Java program from scratch that meets the following requirements: a. The program is...
1. Write a Java program from scratch that meets the following requirements: a. The program is in a file called Duplicates.java that defines a class called Duplicates (upper/lower case matters) b. The program includes a Java method called noDuplicates that takes an array of integers and returns true if all the integers in that array are distinct (i.e., no duplicates). Otherwise it returns false. Make sure the method is public static. example tests: noDuplicates({}) returns true noDuplicates({-1, 1}) returns true...
4 Implement a Java program that meets the following requirements • You can use the Java...
4 Implement a Java program that meets the following requirements • You can use the Java standard sequence data structure API types for sets, lists, stack,queue and priority queue as needed. All are available in the java.util package, which you will want to import in your program. 1. Argue in code comments which data structure, stack or queue, you will use to implement this method. Implement a method which creates some String objects as food orders for a small restaurant,...
Directions: You are to write a C++ program that meets the instruction requirements below. Deliverables: ·...
Directions: You are to write a C++ program that meets the instruction requirements below. Deliverables: · Your C++ source code file. (The file with the .CPP extension).No other files will be accepted. A screenshot of your program running. Program Instructions: Consider the following incomplete C++ program: #include int main() { … } 1. Write a statement that includes the header files fstream, string, and iomanip in this program. 2. Write statements that declare inFile to be an ifstream variable and...
Directions: You are to write a C++ program that meets the instruction requirements below. Deliverables: ·Your...
Directions: You are to write a C++ program that meets the instruction requirements below. Deliverables: ·Your C++ source code file. (The file with the .CPP extension).No other files will be accepted. A screenshot of your program running. Program Instructions: Consider the following incomplete C++ program: #include <iostream> int main() { … } 1.    Write a statement that includes the header files fstream, string, and iomanip in this program. 2.    Write statements that declare inFile to be an ifstream variable and outFile to...
Write a C program that meets the following requirements. Uses a while loop to display the...
Write a C program that meets the following requirements. Uses a while loop to display the first 10 natural numbers (on one row, with a tab separating each number) Uses a while loop to find the sum of the second set of 10 natural numbers. Reads a user entry and displays all the natural numbers up to the user entry (on a column list with a new line separating each number). Finds and displays the sum of all natural numbers...
Write a java program. The program requirements are: the program will let the user input a...
Write a java program. The program requirements are: the program will let the user input a starting number, such as 2.  It will generate ten multiplication problems ranging from 2x1 to 2x10.  For each problem, the user will be prompted to enter the correct answer. The program should check the answer and should not let the user advance to the next question until the correct answer is given to the question being asked currently. After testing a total of 10 multiplication problems,...
Using Python, write a program named hw3b.py that meets the following requirements: Welcome the user to...
Using Python, write a program named hw3b.py that meets the following requirements: Welcome the user to Zion's Pizza Restaurant and ask the user how many people are in their dinner group. If the answer is more than eight (8), print a message saying they'll have to wait for a table. Otherwise, report that their table is ready and take their order. Assume the client orders one pizza, and ask what he/she would like on their pizza, include a loop that...
Using Python, write a program that meets the following requirements: Make a list called sandwich_orders and...
Using Python, write a program that meets the following requirements: Make a list called sandwich_orders and fill it with the names of various sandwiches. Make an empty list called finished_sandwiches. Loop through the list of sandwich orders and spring a message for each order such as "I am working on your tuna sandwich" As each sandwich is made, move it to the list of finished sandwiches. After all the sandwiches have been made, print a message listing each sandwich that...
Using Python, write a program named hw3a.py that meets the following requirements: Create a dictionary called...
Using Python, write a program named hw3a.py that meets the following requirements: Create a dictionary called glossary. Have the glossary include a list of five (5) programing words you have learned in chapters 4-6. Use these words as keys to your dictionary. Find the definition of these words and use them as the values to the keys. Using a loop, print each word and its meaning as neatly formatted output. Create three dictionaries called person. Have each of the person...
Write a program that meets the following requirements: Cat Class Create a class called Cat which...
Write a program that meets the following requirements: Cat Class Create a class called Cat which has only the following instance variables: - name - breed - number of legs - year born Create the no-argument constructor Create the constructor which uses all fields as parameters Write the getter and setter methods for all instance variables Override the toString method using the example shown above There should be NO main method in the Cat class. CatTester Class Create a class...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT