-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMegaBytesConverter.java
More file actions
52 lines (35 loc) · 2.1 KB
/
MegaBytesConverter.java
File metadata and controls
52 lines (35 loc) · 2.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
/********************************************************************************************************************
MegaBytes Converter
===================
Write a method called printMegaBytesAndKiloBytes that has 1 parameter of type int with the name kiloBytes.
The method should not return anything (void) and it needs to calculate the megabytes and remaining kilobytes from the kilobytes parameter.
Then it needs to print a message in the format "XX KB = YY MB and ZZ KB".
XX represents the original value kiloBytes.
YY represents the calculated megabytes.
ZZ represents the calculated remaining kilobytes.
For example, when the parameter kiloBytes is 2500 it needs to print "2500 KB = 2 MB and 452 KB"
If the parameter kiloBytes is less than 0 then print the text "Invalid Value".
EXAMPLE INPUT/OUTPUT
printMegaBytesAndKiloBytes(2500); → should print the following text: "2500 KB = 2 MB and 452 KB"
printMegaBytesAndKiloBytes(-1024); → should print the following text: "Invalid Value" because parameter is less than 0.
printMegaBytesAndKiloBytes(5000); → should print the following text: "5000 KB = 4 MB and 904 KB"
TIP: Be extremely careful about spaces in the printed message.
TIP: Use the remainder operator
TIP: 1 MB = 1024 KB
NOTE: Do not set kilobytes parameter value inside your method.
NOTE: The solution will not be accepted if there are extra spaces.
NOTE: The printMegaBytesAndKiloBytes method needs to be defined as public static like we have been doing so far in the course.
NOTE: Do not add a main method to solution code.
*********************************************************************************************************************/
public class MegaBytesConverter {
public static void printMegaBytesAndKiloBytes(int kiloBytes){
int megaBytes=kiloBytes/1024;
int remKiloBytes=kiloBytes%1024;
if (kiloBytes<0){
System.out.println("Invalid Value");
}
else{
System.out.println(kiloBytes+" KB = "+megaBytes+" MB and "+remKiloBytes+" KB");
}
}
}