USING JAVA Here is the skeleton txt file. y Minnesota Wild on Twitt × 9 Course:
ID: 3730040 • Letter: U
Question
USING JAVA
Here is the skeleton txt file.
y Minnesota Wild on Twitt × 9 Course: FMIS 3232 Com: × cpb18s-hw5.pdf × Implement the main me: × C Secure https//ay17.moodle.umn.edu/pluginfile.php/1657360/mod resource/content/11/cpb18s-hw5.pdf Apps DelUniversity of MinnesConnectMinneapolis/St. Pauumd-fmis.d.umn.edu Dashboard d. 2 points) Reverse the String str via an empty StringBuilder by appending characters in the reverse order, and print "Elapsed Time is xx msec (Using StringBuilder to reverse)" e. (2 points) Reverse the String str by creating a StringBuilder and invoking its reverse), and print "Elapsed Time is xx msec (Using StringBuidler's reverse() method)" 6. Test run your program before you submit your solution as part of a jar file Problem #2 (15 points): Overloading and formatting 1. 2. Add the package hw5p2 to your FirstName-LastName-HW5 project. Add a new class named OLFormatting with a main method to the hw5p2 package. Copy the code of the hw5p2Skeleton.txt file posted on the Moodle course site and paste it on to your class file. Complete the OLFormatting class as follows: 3. a. Complete the segments indicated by short directions (e.g., add a parameter list here) as well as marked by numbers (ie, #1, #2, and so on) while referring to the argument lists and outputs given in the "documentation comments" for the methods. One of the methods does not need a parameter list. All the methods must be overloaded b. a. The main method must only call the methods of the OLFormatting.java class. b. Hints: Arrays and date/time arguments must be prepared before calling methods. Use 4. Write code for the main of the OLFormatting.java as follows: LocalDateTime class to get the current local date and time 5. 6. The outputs from each method have to match the given outputs. Test run your program before you submit your solution as part of a jar file How to turn in your homework? See the homework assignment #1 directions. Make sure that the JRE System Library is included in your JAR file. eg 5:49 PM 3/14/2018 24Explanation / Answer
package hw5p2;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Locale;
public class OLFormatting {
public static void main(String[] args) {
// Invoke the methods below to print output
String[] colors = { "apricot", "yellow", "green", "black" };
String[] animals = { "rhino", "koala", "rabbit", "deer" };
// reverseNums(10, 20, 30, 40);
// formatDoubles(10, 20, 30, 40);
// padZeros();
// roundUp(3.14159);
// concattStrings("raseaC", "suiluJ");
// padRightLeft(colors, animals);
displayDateAndTime(Calendar.getInstance());
}
/**
* Call String.format with three integer arguments and
* display them in reverse order.
* @param num1 10
* @param num2 20
* @param num3 30
* @param num4 40
*
* Output:
* Fourth: 40
* Third: 30
* Second: 20
* First: 10
*/
public static void reverseNums(int num1,int num2,int num3,int num4){ //#1
String reverse = String.format("Fourth: %d Third: %d Second: %d First: %d",num4,num3,num2,num1); //#2
System.out.println(reverse);
}
/**
* Call String.format with three integer arguments and format
* them to doubles.
* @param num1 10
* @param num2 20
* @param num3 30
* @param num4 40
*
* Output:
* First: 10.00
* Second: 20.00
* First: 30.00
* Fourth: 40.00
*/
public static void formatDoubles(int num1, int num2, int num3, int num4){ //#3
//Using decimal formatter to format integers upto 2 decimal places
DecimalFormat df = new DecimalFormat("#.00");
String result = String.format("First: %s Second: %s Third: %s Fourth: %s",df.format((double)num1),df.format((double)num2),df.format((double)num3),df.format((double)num4)); //#4
System.out.println(result);
}
/**
* Pad with zeros and a width of 5 chars.
* @param (none)
*
* Output:
* 00005 00015
* 00004 00014
* 00003 00013
* 00002 00012
* 00001 00011
*/
public static void padZeros(){ //#5
for (int i = 5; i > 0; i--) {
StringBuilder sb = new StringBuilder(String.format("%05d", i));
//Printing the first column is simply by padding zeros before the numbers.
//For the second column, using the formatting code from the first column and
//changing the fourth character to make it 1, since it is given that all inputs
//are single digit.
sb.setCharAt(3, '1');
String result = String.format("%05d %s",i,sb); //#6
System.out.println(result);
}
}
/**
* Round the number up and display it with 2 decimal places
* @param num 3.14159
*
* Output:
* 3.14
*/
public static void roundUp(double num){ //#7
DecimalFormat df = new DecimalFormat("#.00");
// Before formatting, multiplying and dividing by 100.0 to get
// correct rounded off value because Math.round rounds off to the nearest integer value
String result = String.format("%s",df.format(Math.round(num*100.0)/100.0)); //#8
System.out.println(result);
}
/**
* Reverse the name strings and display in the order of capitalized
* FirstName LastName.
* @param name1 raseaC
* @param name2 suiluJ
*
* Output:
* JULIUS CAESAR
*/
public static void concattStrings(String name1,String name2){ //#9
StringBuilder builder1 = new StringBuilder(name1);
StringBuilder builder2 = new StringBuilder(name2);
//------------------------------------------------------------ //#10
//write code her to reverse the values of the two string args
//------------------------------------------------------------
String reverse1 = builder1.reverse().toString().toUpperCase(); //Converting the StringBuilder to a string to apply toUpperCase function
String reverse2 = builder2.reverse().toString().toUpperCase(); //Converting the StringBuilder to a string to apply toUpperCase function
String result = String.format("%s %s",reverse2,reverse1); //#11
System.out.println(result);
}
/**
* Pad left and right and a width of 10 characters.
* @param colors { "apricot", "yellow", "green", "black" }
* @param animals { "rhino", "koala", "rabbit", "deer" }
*
* Output:
* apricot rhino
* yellow koala
* green rabbit
* black deer
*/
public static void padRightLeft(String[] colors, String[] animals){ //#12
for (int i=0;i<colors.length;i++) {
//------------------------------------------------------ //#13
//write code here to retrieve an element fromeach array
//------------------------------------------------------
//Print them with a 10-space gap between the strings
String result = String.format("%s %s",colors[i],animals[i]); //#14
System.out.println(result);
}
}
/**
* Format date and time
* @param cal calendar (an instance of Calendar)
*
* Output: (It will vary depending on the date you set and
* time you run the program)
* Month: October Day: 29 Year: 2017
* Day: Sunday Sun 29
* Year: 2017 17 301
* Hour: 11 11 (note: 24- and 12-based time, respectively)
* Minute: 43
* Second: 21
* AM/PM: am
*/
public static void displayDateAndTime(Calendar cal){ //#15
SimpleDateFormat dateFormat = new SimpleDateFormat("EEEEE"); //Using SimpleDateFormat to specify format for getting Weekday in String
String result = String.format("Month: %s Day: %d Year: %d",cal.get(Calendar.MONTH),cal.get(Calendar.DAY_OF_MONTH),cal.get(Calendar.YEAR)); //#16
System.out.println(result);
result = String.format("Day: %s %s %d",dateFormat.format(cal.getTime()),cal.getDisplayName(Calendar.DAY_OF_WEEK, Calendar.SHORT, Locale.US),cal.get(Calendar.DAY_OF_MONTH)); //#17
System.out.println(result);
result = String.format("Year: %d %d %d",cal.get(Calendar.YEAR),cal.get(Calendar.YEAR)%2000,cal.get(Calendar.DAY_OF_YEAR)); //#18
System.out.println(result);
result = String.format("Hour: %d %d",cal.get(Calendar.HOUR_OF_DAY),cal.get(Calendar.HOUR)); //#19
System.out.println(result);
result = String.format("Minute: %d",cal.get(Calendar.MINUTE)); //#20
System.out.println(result);
result = String.format("Second: %d",cal.get(Calendar.SECOND)); //#21
System.out.println(result);
result = String.format("AM/PM: %s",cal.get(Calendar.AM_PM) == 0 ? "am": "pm"); //Calendar.AM_PM returns a number 0/1 depending on whether the HOUR is before or after noon. Using ternary operator to quickly check and print am or pm based on returned value //#22
System.out.println(result);
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.