W2 Tepkro (PR) v2 2022
W2 Tepkro (PR) v2 2022
W2 Tepkro (PR) v2 2022
_Selamat Mengerjakan_
package addnum;
import java.io.*;
/**
* <h1>Add Two Numbers!</h1>
* The AddNum program implements an application that
* simply adds two given integer numbers and Prints
* the output on the screen.
* <p>
* <b>Note:</b> Giving proper comments in your program makes it more
* user friendly and it is assumed as a high quality code.
*
* @author Zulkifli
* @version 1.0
* @since 2022-02-09
*/
/**
* This method is used to add two integers. This is
* a the simplest form of a class method, just to
* show the usage of various javadoc Tags.
* @param numA This is the first paramter to addNum method
* @param numB This is the second parameter to addNum method
* @return int This returns sum of numA and numB.
*/
public int addNum(int numA, int numB) {
return numA + numB;
}
/**
* This is the main method which makes use of addNum method.
* @param args Unused.
* @exception IOException On input error.
* @see IOException
*/
Java has 8 primitive data types; char, boolean, byte, short, int, long, float, and double. For this
exercise, we'll work with the primitives used to hold integer values (byte, short, int, and long):
• A byte is an 8-bit signed integer.
• A short is a 16-bit signed integer.
• An int is a 32-bit signed integer.
• A long is a 64-bit signed integer
Given an input integer, you must determine which primitive data types are capable of properly
storing that input.
Input Format
The first line contains an integer, T, denoting the number of test cases. Each test case, T, is
comprised of a single line with an integer, n, which can be arbitrarily large or small.
Output Format
For each input variable n and appropriate primitive datatype, you must determine if the given
primitives are capable of storing it. If yes, then print:
If there is more than one appropriate data type, print each one on its own line and order them
by size (i.e.: byte < short < int < long).
If the number cannot be stored in one of the four aforementioned primitives, print the line:
Sample Input:
5
-150
150000
1500000000
213333333333333333333333333333333333
-100000000000000
Explanation:
150 can be stored in a short, an int, or a long.
213333333333333333333333333333333333 is very large and is outside of the allowable
range of values for the primitive data types discussed in this problem.
Class FloatingPoint{
public static void main(String[] args) {
double x = 92.98;
int nx = (int) Math.round(x);
}
}
Math Class berisi bermacam-macam fungsi matematika seperti pada contoh diatas pada
penggunaan round(x), terdapat beberapa pertanyaan yang perlu untuk dijelaskan:
1. Pada kasus berikut jelaskan nilai nx setelah digunakan Math.round(x);
2. Kenapa dibutuhkan cast (int) dalam penggunaan Math.round(x) ?
class ConvertDataType
{
static short methodOne(long l)
{
int i = (int) l;
return (short)i;
}
public static void main(String[] args)
{
double d = 10.25;
float f = (float) d;
byte b = (byte) methodOne((long) f);
System.out.println(b);
}
}
Program berikut melakukan convert tipe data yang berukuran besar ke kecil (long -> int ->
short) dan (double -> float -> byte).
1. Jelaskan output nilai dari variable b.
2. Jelaskan apa yang berubah dari variable d menjadi variable b setelah dilakukan cast ?
This exercise is to test your understanding of Java Strings. A sample String declaration:
The elements of a String are called characters. The number of characters in a String is called
the length, and it can be retrieved with the String.length() method.
Given two strings of lowercase English letters, A and B, perform the following operations:
1. Sum the lengths of A and B.
2. Determine if A is lexicographically larger than B (i.e: does B come before A in the
dictionary?)
3. Capitalize the first letter in A and B and print them on a single line, separated by a
space.
Input Format
The first line contains a string A. The second line contains another string B. The strings are
comprised of only lowercase English letters.
Output Format
There are three lines of output:
For the first line, sum the lengths of A and B.
For the second line, write Yes if A is lexicographically greater than B otherwise print No
instead.
For the third line, capitalize the first letter in both A and and B print them on a single line,
separated by a space.
Sample Input 0
hello
java
Sample Output 0
9
No
Hello Java
When you capitalize the first letter of both A and B and then print them separated by a space,
you get "Hello Java".