Question

JAVA Programming

Please give the code without any "+" operator or "println"!

Your solution must only use printf to display output. Autolab will automatically reject submissions that call print or println. Your solution must also NOT use the + operator.

OutputUtil.java contains stubs for three methods: printMoney, printTime, and main. Implement the print methods using one line of code each. In the mainmethod, call printMoney and printTime (twice each) to produce the following output:

$1.20
3.45 CHF
7:30:00
12:08:03

Neither of the print methods should display a newline character, so that they can be called in the middle of a sentence. For this exercise, the main method will print newlines after each method call.

1 0
Add a comment Improve this question Transcribed image text
✔ Recommended Answer
Answer #1
public class Main {
  
public static void main(String[] args)
{
printMoney("$", 1.2, "");
System.out.printf("\n");
printMoney("", 3.45, "€");
System.out.printf("\n");
  
printTime(7, 30, 0);
System.out.printf("\n");
printTime(12, 8, 3);
System.out.printf("\n");
}
  
public static void printMoney(String prefix, double value, String suffix)
{
if(prefix.equals(""))
System.out.printf("%-,3.2f %-1s", value, suffix);
else
System.out.printf("%-1s %-,3.2f", prefix, value);
}
  
public static void printTime(int hour, int minute, int second)
{
if(hour < 0 || hour > 23 || minute < 0 || minute > 59 || second < 0 || second > 59)
{
System.out.printf("Invalid time format!\n");
return;
}
  
String sec;
if(second < 10)
sec = "0" + second;
else
sec = second + "";
  
String min;
if(minute < 10)
min = "0" + minute;
else
min = minute + "";
  
String time = hour + ":" + min + ":" + sec;
System.out.printf(time);
}
}


answered by: Zahidul Hossain
Add a comment
Know the answer?
Add Answer to:
JAVA Programming
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Similar Homework Help Questions
  • Assume the only console output method available is display(c), as shown below, which you include in...

    Assume the only console output method available is display(c), as shown below, which you include in your solution. This means you are not permitted to use System.out.print(), System.out.println(), nor System.out.printf() in your methods. public static void display (charc) { System.out.print (c); Implement the following methods in a program class, that has a main() method that simply tests each. Apply the concept of overloading, where best, and that the methods can call other new methods to reduce repeating the same code....

  • Please help me. package a1; public class Methods {    /*    * Instructions for students:...

    Please help me. package a1; public class Methods {    /*    * Instructions for students: Use the main method only to make calls to the    * other methods and to print some testing results. The correct operation of    * your methods should not depend in any way on the code in main.    *    * Do not do any printing within the method bodies, except the main method.    *    * Leave your testing code...

  • All JAVA code 6.2.2: Define a method printFeetInchShort, with int parameters numFeet and numInches, that prints...

    All JAVA code 6.2.2: Define a method printFeetInchShort, with int parameters numFeet and numInches, that prints using ' and " shorthand. Ex: printFeetInchShort(5, 8) prints: 5' 8" Hint: Use \" to print a double quote Sample program: import java.util.Scanner; public class HeightPrinter { /* Your solution goes here */ public static void main (String [] args) { printFeetInchShort(5, 8); System.out.println(""); return; } } 6.4.1: Define stubs for the methods called by the below main(). Each stub should print "FIXME: Finish...

  • Please write this in java Write one static method to print each character in the output....

    Please write this in java Write one static method to print each character in the output. There should be methods to print: H, E, L, O, (comma), W, R, D and (exclamation point). The method to print H should be called printH() and the method to print the comma should be called printComma(). Make sure to separate characters with a new line in the method (except the comma) as shown on right. Write a method called printHelloWorld() to call the...

  • THE LANGUAGE IS JAVA!! Define stubs for the methods called by the below main(). Each stub...

    THE LANGUAGE IS JAVA!! Define stubs for the methods called by the below main(). Each stub should print "FIXME: Finish methodName'followed by a newline, and should return -1. Example output: FIXME: Finish getUserNum() FIXME: Finish getUserNum() FIXME: Finish computeAvg() Avg: -1 import java.util.Scanner; 3 public class MthdStubsStatistics { 1* Your solution goes here */ public static void main(String [] args) { int userNum1; int userNum2; int avgResult; userNum1 = getUserNum(); userNum2 = getUserNum(); avgResult = computeAvg(userNumi, userNum2); System.out.println("Avg: " +...

  • The Language is Java GenericMethodTest (20) Download the OverloadedMethods.java program. Replace all the methods except main...

    The Language is Java GenericMethodTest (20) Download the OverloadedMethods.java program. Replace all the methods except main with a single generic method that produces the same output. Call the new file GenericMethodsTest.java. OverloadMethods.java: // Printing array elements using overloaded methods. public class OverloadedMethods { // method printArray to print Integer array public static void printArray(Integer[] inputArray) { // display array elements for (Integer element : inputArray) { System.out.printf("%s ", element); } System.out.printf("\n"); } // method printArray to print Double array public...

  • in java : Create Java Application you are to create a project using our designated IDE...

    in java : Create Java Application you are to create a project using our designated IDE (which you must download to your laptop). Create the code to fulfill the requirements below. Demonstrate as stipulated below. Create a Main Application Class called AddressBookApplication (a counsole application / command line application). It should Contain a Class called Menu that contains the following methods which print out to standard output a corresponding prompt asking for related information which will be used to update...

  • Define stubs for the methods called by the below main(). Each stub should print "FIXME: Finish...

    Define stubs for the methods called by the below main(). Each stub should print "FIXME: Finish methodName()" followed by a newline, and should return -1. Example output: FIXME: Finish getUserNum() FIXME: Finish getUserNum() FIXME: Finish computeAverage() Avg: -1 import java.util.Scanner; public class MethodStubs{ /* Your solution goes here */ public static void main(String [] args) { int userNum1; int userNum2; int avgResult; MethodStubs stubTester = new MethodStubs(); userNum1 = stubTester.getUserNum(); userNum2 = stubTester.getUserNum(); avgResult = stubTester.computeAverage(userNum1, userNum2); System.out.println("Avg: " +...

  • (Packing Characters into an Integer) The left-shift operator can be used to pack four character values into a four-byt...

    (Packing Characters into an Integer) The left-shift operator can be used to pack four character values into a four-byte unsigned int variable. Write a program that inputs four characters from the keyboard and passes them to function packCharacters. To pack four characters into an unsigned int variable, assign the first character to the unsigned intvariable, shift the unsigned int variable left by 8 bit positions and combine the unsigned variable with the second character using the bitwise inclusive OR operator....

  • This looks long but it is easy just having some trouble please help out thank you....

    This looks long but it is easy just having some trouble please help out thank you. Find and fix all syntax and semantic errors which prevent the program from compiling. Find and fix all logical errors which cause the program to crash and/or produce unexpected results. In addition to making sure the code compiles, we have one final method which we need to complete. There is a method in the code, printAll, which is responsible for printing out the entirety...

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