In: Computer Science
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
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(); } } |