0% found this document useful (0 votes)
59 views

ICM104 Kavinda Peiris - Java Literals - Assignment Week 02

1. The document discusses Java literals and provides examples of using different literal types like integer, double, string, and character literals in Java programs. 2. Examples are given to demonstrate printing output to the console using println() and print() methods and using escape sequences in string literals. 3. The document also discusses valid and invalid number literal formats for binary, octal and hexadecimal literals and examples are provided to illustrate escape sequences and string formatting.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
59 views

ICM104 Kavinda Peiris - Java Literals - Assignment Week 02

1. The document discusses Java literals and provides examples of using different literal types like integer, double, string, and character literals in Java programs. 2. Examples are given to demonstrate printing output to the console using println() and print() methods and using escape sequences in string literals. 3. The document also discusses valid and invalid number literal formats for binary, octal and hexadecimal literals and examples are provided to illustrate escape sequences and string formatting.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

Week02 – Programing Fundamentals

Java Literals
15th July 2023

1. class Example{
public static void main(String []args){
System.out.println("Kavinda Peiris");
}
}

Output –
H:\ICM\Assignments\Day 2\Java Literals>java Q1
Kavinda Peiris

2. class Q2{
public static void main(String []args){
System.out.println("Kavinda Peiris");
System.out.println("No:122, Senanayake Road, Bandarawela");
System.out.println("0773279280");
System.out.println("[email protected]");
}
}

Output –
H:\ICM\Assignments\Day 2\Java Literals>java Q2
Kavinda Peiris
No:122, Senanayake Road, Bandarawela
0773279280
[email protected]

3. class Q3{
public static void main(String []args){
System.out.println("*");
System.out.println("* *");
System.out.println("* * *");
System.out.println("* * * *");
}
}

Output –
H:\ICM\Assignments\Day 2\Java Literals>java Q3
*
**
***
****

4. class Q4{
public static void main(String []args){
System.out.println(" 299");
System.out.println("+ 800");
System.out.println("-------------");
System.out.println(" 1099");
1|Page iCM104 – Kavinda Peiris
System.out.println("=============");
}
}

Output –
H:\ICM\Assignments\Day 2\Java Literals>java Q4
299
+ 800
-------------
1099
=============

5. class Q5{
public static void main(String []args){
System.out.println("* * * * * * =======================================");
System.out.println(" * * * * * =======================================");
System.out.println("* * * * * * =======================================");
System.out.println(" * * * * * =======================================");
System.out.println("* * * * * * =======================================");
System.out.println(" * * * * * =======================================");
System.out.println("* * * * * * =======================================");
System.out.println(" * * * * * =======================================");
System.out.println("* * * * * * =======================================");
System.out.println("===================================================");
System.out.println("===================================================");
System.out.println("===================================================");
System.out.println("===================================================");
System.out.println("===================================================");
System.out.println("===================================================");
}
}

Output –
H:\ICM\Assignments\Day 2\Java Literals>java Q5
* * * * * * =======================================
* * * * * =======================================
* * * * * * =======================================
* * * * * =======================================
* * * * * * =======================================
* * * * * =======================================
* * * * * * =======================================
* * * * * =======================================
* * * * * * =======================================
===================================================
===================================================
===================================================
===================================================
===================================================
===================================================

6. Program A
H:\ICM\Assignments\Day 2\Java Literals>java Example
Institute of Computer Engineering Technology
223 A,
2|Page iCM104 – Kavinda Peiris
Galle Road,
Panadura.

Program B
H:\ICM\Assignments\Day 2\Java Literals>java Example
Institute of Computer Engineering Technology
223 A,Galle Road,Panadura.

7. If we want cursor to go to the next line after displaying something we can use “println”. But if we don’t want to
go to the next line after displaying something we can use “print”.

Lets assume this example


class Example{
public static void main(String []args){
System.out.println(“Hello”);
System.out.print(“World”);
System.out.pint(“!”);
}
}

In this code first display ‘Hello’ and then go to the next line and then display ‘World’ with ‘!’ as a single world
like ‘World!’.

8. In line 1, “a” is string type and the second line ‘a’ is a character type.

9. Line 1 has integer type


Line 2 has double type
Line 3 has string type
Line 4 has character type

10. Types of java literals are integer, floating-point, string, character, Boolean and null literals.
Example of literals
class JavaLiterals{
public static void main(String []args){
int x=8;
double y=8.0;
char z='z';
String a="Kavinda";
boolean bool = true;
}
}

11. Line 1 display normal number, second line display decimal value of the given binary value, third line display octal
value and last line display hexadecimal value of the given value separate lines.

12. Line 1 – valid, because of starts with 0B means it is a binary number


Line 2 – valid, because of starts with 0b means it is also a binary number
Line 3 – not valid, because of it’s start with 0B that mean binary number, but binary number allow only 1 and 0. It
has 2. So it’s not allow.
Line 4 – valid, because of it’s start with 0 and it is a octal value. So it’s valid
Line 5 – not valid, because of it’s start with 0 and it’s a octal number. So octal number only allow 0 to 7. It has 8.
So it’s not valid.
Line 6 – valid, because of it’s start with 0x. That means it’s a hexadecimal number. So it’s valid.

3|Page iCM104 – Kavinda Peiris


Line 7 – valid, because of it’s start with 0x. So it’s hexadecimal value. Hexadecimal values can allow 0 to 9 and a
to f. So it’s valid.
Line 8 – not valid, because of it’s start with 0x. So it’s a hexadecimal value. Hexadecimal values can allow 0 to 9
and a to f. So it has g. g is not valid.
Line 9 – not valid, because it’s hexadecimal type without any value. So it’s not valid.
Line 10 – valid, because of it’s start with 0x. So it’s hexadecimal value. Hexadecimal values can allow 0 to 9 and
a to f. So it’s valid.

13. Result should be like this:-


A
BCD

EF
G
H

14. i. class Example{


public static void main(String args[]){
System.out.print("iCM - iCET CERTIFIED MASTER");
}
}

ii. class Example{


public static void main(String args[]){
System.out.print("iCM \n\niCET \nCERTIFIED \nMASTER");
}
}

iii. class Example{


public static void main(String args[]){
System.out.print("iCM \n\niCET CERTIFIED MASTER");
}
}

15. Output –
Hello
JAVA
Hello JAVA
HelloJAVA
\Hollo JAVA\
“Hello
JAVA”
‘Hello
JAVA’

Escape Sequences
\n – cursor got to the next line
\t – make tab space
\b – remove single space or letter before ‘\b’ that. Ex “Hello \bWorld” prints like a “HelloWorld”.
\\ - display back slash
\” – display double quotes
\’ – display single quotes

4|Page iCM104 – Kavinda Peiris


16. a. System.out.print(“Java is a typed language”);
b. System.out.print(“AB\”CB”);
c. System.out.print(“AB\\CD”);
d. System.out.print(“C:\\Windows\\Programs”);
e. System.out.print(“AB\\\”CD”);
f. System.out.print(“AB\\\\\”\”CD”);
g. System.out.print(“AB\\nCD”);
h. System.out.print(“AB\\tCD”);
i. System.out.print(“AB\\bCD”);

17. class Example {


public static void main(String args[]){
System.out.println(" _ ______ ________ __________ ");
System.out.println(" ( )/ ____| ______|___ ___| ");
System.out.println(" _| | | |___ | | ");
System.out.println(" | | | | ___| | | ");
System.out.println(" | | |____| |______ | | ");
System.out.println(" |_|\\______|________| |__| ");
}
}

18. class Example {


public static void main(String args[]){
System.out.println(" X ");
System.out.println(" / \\ ");
System.out.println(" / \\ ");
System.out.println(" / \\ ");
System.out.println(" / \\ ");
System.out.println(" / \\ ");
System.out.println(" / \\ ");
System.out.println(" \'\'\'\'\'\'\'\'\'\'\'\'\'\'\' ");
System.out.println(" ___|_|___ ");
}
}

19. class Example {


public static void main(String args[]){
System.out.println(" +\"\"\"\"\"+ ");
System.out.println(" [| O O |] ");
System.out.println(" | ^ | ");
System.out.println(" | \'-\' | ");
System.out.println(" +\'\'\'\'\'+ ");
System.out.println(" ||||||||| ");
System.out.println(" /\\/\\/|||||||||||\\/\\/\\ ");
System.out.println(" ||||||||||||| ");
System.out.println(" ||||||||||||||| ");
System.out.println(" ||||||||||||| ");
System.out.println(" ||||||||||| ");
System.out.println(" ||||||||| ");
System.out.println(" /\\ /\\ ");
}
}

5|Page iCM104 – Kavinda Peiris


20. In line 1, declare a integer type variable i.
In line 2, assign value 103 to the declared integer type variable i.
In line 3, display the value of i.

21. class Example {


public static void main(String args[]){
int x = 102, y = 103;
System.out.println(x+", "+y);
System.out.println(y+", "+x);
}
}

22. compile time error

23. Output-
10
20
30
200

24. Output –
100
200
100
300

Explanation – First declare two integer type variable x and y. Then value 100 assign to x and value 200 assign to
y. After that Print x and y. Then again value 300 assign to y. So now value of y is 300. Then again print x and y.

25. B is the legal answer. A is illegal because there has no any value assign to x. C and D are also illegal because in
code x is declared as integer type variable. So it’s not allow declare same variable two times. E is also incorrect
because if we insert nothing it shows error because of x has no any value.

26. D. Compile error at line 6. Because in line 6 it’s not allow “int i=j=k=10” in java. It’s valid like this “int i,j,k=10”.

27. Output –
1020
30

Explanation – In line 1, two strings concatenate. In line 2, value 20 added to the value 10 and display the result.

28. Output –
In this code ‘pubic’ keyword missing ‘l’. So it display error message only. But if “pubic” replace with
“public” it’s output like this.

60
10+20+30
10+2030
102030
1050
3030
102030

6|Page iCM104 – Kavinda Peiris


29. A. its output is ‘6’. Because inside the print function calculate the expression before display it.
B. its output is ‘123’. Because concatenate 3 strings.
C. its output is ‘123’. Because it’s concatenate 3 characters
D. its output is ‘1 2 3’. Because it’s concatenate 3 characters and two strings
E. its output is ‘198’.
F. its output is ‘ABC’. Because concatenate 3 strings.
G. its output is ‘365’.
H. its output is ‘A B C’. Because it’s concatenate 3 characters and two strings

30. In question 20 value 103 assigned to the integer type x variable. But in here some value is assigned by the user
input. But this code has missing one line “Scanner input = new Scanner(System.in);”

31. import java.util.Scanner;


class Example{
public static void main(String[] args){
Scanner scanner = new Scanner(System.in);
//first input
System.out.print("1st input - ");
int x = scanner.nextInt();
//second input
System.out.print("2nd input - ");
int y = scanner.nextInt();
//display x and y
System.out.println("\n1st input -"+x);
System.out.println("2nd input -"+y);
//calculate the sum of x and y
int sum = x+y;
//display it
System.out.println(x+" "+y+" = "+sum);
}
}

32. import java.util.Scanner;


class Example{
public static void main(String[] args){
Scanner scanner = new Scanner(System.in);
System.out.print("Input number: ");
int num = scanner.nextInt();
System.out.print(num);
System.out.print(num);
System.out.print(num);
System.out.println("\n"+num*3);
}
}

7|Page iCM104 – Kavinda Peiris


33. import java.util.Scanner;
class Example{
public static void main(String[] args){
Scanner scanner = new Scanner(System.in);
//get input Combined Maths marks
System.out.print("Input Combined Maths marks: ");
int maths = scanner.nextInt();
//get input Chemistry marks
System.out.print("Input Chemistry marks: ");
int chem = scanner.nextInt();
//get input Physics marks
System.out.print("Input Physics marks: ");
int phy = scanner.nextInt();
//get input English marks
System.out.print("Input English marks: ");
int eng = scanner.nextInt();
//total
int tot = maths+chem+phy+eng;
//output
System.out.println("\t\nCombined Maths - "+maths);
System.out.println("\tChemistry - "+chem);
System.out.println("\tPhysics - "+phy);
System.out.println("\tEnglish - "+eng);
System.out.println("\tTotal - "+tot);
}
}

34. import java.util.Scanner;


class Example{
public static void main(String[] args){
Scanner scanner = new Scanner(System.in);
//get some value for Red
System.out.print("Enter Red value: ");
int red = scanner.nextInt();
//get some value for Green
System.out.print("Enter Green value: ");
int green = scanner.nextInt();
//get some value for Blue
System.out.print("Enter Blue value: ");
int blue = scanner.nextInt();
//new values
int new_red = 255 - red;
int new_green = 255 - green;
int new_blue = 255 - blue;
//display output
System.out.println("Inversion of given colour - ["+new_red+", "+new_green+",
"+new_blue+"]");
}
}

8|Page iCM104 – Kavinda Peiris


35.
Data Type Size Description

byte 1 byte -128 to 127


short 2 bytes -32 768 to 32 767
int 4 bytes -2 147 483 648 to 2 147 483 647
long 8 bytes -9,223,372,036,854,775,808 to
9,223,372,036,854,775,807
float 4 bytes 6 to 7 decimal points
double 8 bytes 15 decimal points
boolean 1 bit true or false
char 2 bytes Single character or ASCII values

36. A. legal – bytes allow -128 to 127


B. not legal - bytes allow -128 to 127
C. legal - bytes allow -128 to 127
D. legal - bytes allow -128 to 127
E. legal - bytes allow -32 768 to 32 767
F. not legal - bytes allow -32 768 to 32 767
G. legal - bytes allow -32 768 to 32 767
H. legal
I. legal
J. not legal
K. not legal
L. not legal
M. legal
N. legal
O. legal – allow true or false
P. not legal – it should be false
Q. legal - allow true or false
R. not legal – it’s not allow strings
S. not legal – it’s only allow true or false
T. legal

37. A. l = 1147383947;
C. l = 0xbacc;
D. l = 04032;
E. l = 0101110110L;
All are legal.

38. new, My Variable, @name, user-input, main, $percent, 123num, java.org are not valid identifiers.

39. Output:
32767
-32768

9|Page iCM104 – Kavinda Peiris


Float
class Example{
public static void main(String args[]){
float x;
x=Float.MAX_VALUE;
System.out.println(x);
x=Float.MIN_VALUE;
System.out.println(x);
}
}

---------------------------------------------------------------------
Double
class Example{
public static void main(String args[]){
double x;
x=Double.MAX_VALUE;
System.out.println(x);
x=Double.MIN_VALUE;
System.out.println(x);
}
}

--------------------------------------------------------------------
Int
class Example{
public static void main(String args[]){
int x;
x=Integer.MAX_VALUE;
System.out.println(x);
x=Integer.MIN_VALUE;
System.out.println(x);
}
}

40. Output:
A
66
A1
a
98
a1

41. a. 6 – after solve the expression answer become 6


b. 123 – concatenate integers and character
c. 33 – adding 1 and 2 and their answer concatenate with character 3
d. 123 – concatenate 3 characters
e. 15 – character 1 concatenate with value 5
f. error – A has no assign any value
g. 1h30 – concatenate 2 integers and a character
h. ABC - concatenate 3 characters
i. 123 - concatenate 3 characters

10 | P a g e iCM104 – Kavinda Peiris


42. import java.util.Scanner;
class Example{
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
//get some value from the user
System.out.print("Enter number between 33 and 126 : ");
int input_value = scanner.nextInt();
//checking entered number between 33 and 126
if (input_value>32 && input_value<127){
char ascii_ch = (char) input_value;
System.out.println("ASCII character for "+input_value+" is : "+ascii_ch);
}
else{
System.out.println("Invalid input");
}
}
}

43. “char a = '\u0061';” and “char \u0061 = 'a';” and “ch\u0061r a = 'a';” are correct answers.

44. Output:
true
true
true
true
true
true
true

explanation : In first declare a character type a variable and assign ‘a’ as the value. Then compare a with ‘\u0061’
value. This is true because both are equals. That’s why it display true. Other lines works same as the first one.

45. In line 1 display some text inside the double quotes and line 2 display the value of string type s.

46. import java.util.Scanner;


class Example{
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
//get user's name
System.out.print("Enter your name : ");
String name = scanner.nextLine();
//output part
System.out.println("Hello "+name+", nice to meet you!");
}
}

47. Output:
z is: 131y105z

11 | P a g e iCM104 – Kavinda Peiris


48. import java.util.Scanner;
class Example{
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);

//enter friend's name


System.out.print("Enter your friend\'s name: ");
String f_name = scanner.nextLine();

//enter friend's age


System.out.print("Enter your friend\'s age: ");
int age = scanner.nextInt();

scanner.nextLine();

//enter friend's place


System.out.print("Enter your friend\'s living place: ");
String place = scanner.nextLine();

//output part
System.out.println(f_name+" is my best friend. He is "+age+" old and lives in the beautiful town
of "+place+".");
}
}

49. import java.util.Scanner;


class Example{
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);

System.out.println("----Subject total calculator----");


//enter subject name
System.out.print("\nSubject 1 name - ");
String sub1 = scanner.nextLine();

//enter marks
System.out.print(sub1+" marks - ");
int mark1 = scanner.nextInt();

//new line character


scanner.nextLine();

//enter subject name


System.out.print("Subject 2 name - ");
String sub2 = scanner.nextLine();

//enter marks
System.out.print(sub2+" marks - ");
int mark2 = scanner.nextInt();

//new line character


scanner.nextLine();

//enter subject name


12 | P a g e iCM104 – Kavinda Peiris
System.out.print("Subject 1 name - ");
String sub3 = scanner.nextLine();

//enter marks
System.out.print(sub3+" marks - ");
int mark3 = scanner.nextInt();

//total calculation
int tot = mark1 + mark2 + mark3;

//output part
System.out.println("\n\n"+sub1+"\t\t"+mark1);
System.out.println(sub2+"\t\t"+mark2);
System.out.println(sub3+"\t"+mark3);
System.out.println("\nTotal\t\t"+tot);
}
}

50. import java.util.*;


class Example{
public static void main(String args[]){
Scanner input=new Scanner(System.in);

System.out.print("Input number 1 : ");


int num1=input.nextInt();

System.out.print("Input number 2 : ");


int num2=input.nextInt();

System.out.println(num1+" "+num2);

int num3 = num1;


num1 = num2;
num2 = num3;
System.out.println(num1+" "+num2);
}
}

13 | P a g e iCM104 – Kavinda Peiris

You might also like