Remedial Tutorial 1 Solution
Remedial Tutorial 1 Solution
Bitwise Operators:
1) Bitwise NOT (~): called bitwise complement, the unary NOT operator,
inverts all of the bits of its operand.
Example: ~ 0111 (decimal 7)
2) Bitwise = 1000 (decimal 8) AND (&): the AND
operator, &, produce a 1 bit if both operands are also 1, A zero is
produced in all the cases.
e.g 0101 (decimal 5)
& 0011 (decimal 3)
= 0001 (decimal 1)
3) Bitwise OR( | ) : the OR operator, | , combines bits such that
if either of the bits in the operand is a 1, then the resultant bit is
a1
e.g 0101 (decimal 5)
| 0011 (decimal 3)
= 0111 (decimal 7)
4) Bitwise XOR( ^ ) : the XOR operator , ^ , combines bits
such that if exactly one operand is 1,then the result is 1.
Otherwise result is zero.
e.g 0101 (decimal 5)
^ 0011 (decimal 3)
= 0110 (decimal 6)
5) The Left Shift (<<): the left shift operator, <<, shifts all of
the bits in a ‗value‘ to the left a specified number of times
specified by ‗num‘
General form : value <<num
e.g. x << 2 (x=12)
0000 1100 << 2
= 0011 0000 (decimal
48)
6) The Right Shift (>>): the right shift operator, >>, shifts all
of the bits in a ‗value‘ to the right a specified number of times
specified by ‗num‘
General form: value >>num.
e.g. x>> 2 (x=32)
0010 0000 >> 2
= 0000 1000 (decimal
8)
2) Write a program lo display ASCII value of a number 9.
Ans:
Ans:
import java.util.Scanner;
public class ATM_Transaction
{
public static void main(String args[] )
{
int balance = 5000, withdraw, deposit;
Scanner s = new Scanner(System.in);
while(true)
{
System.out.println("Automated Teller Machine");
System.out.println("Choose 1 for Withdraw");
System.out.println("Choose 2 for Deposit");
System.out.println("Choose 3 for Check Balance");
System.out.println("Choose 4 for EXIT");
System.out.print("Choose the operation you want to
perform:");
int n = s.nextInt();
switch(n)
{
case 1:
System.out.print("Enter money to be withdrawn:");
withdraw = s.nextInt();
if(balance >= withdraw)
{
balance = balance - withdraw;
System.out.println("Please collect your money");
}
else
{
System.out.println("Insufficient Balance");
}
System.out.println("");
break;
case 2:
System.out.print("Enter money to be deposited:");
deposit = s.nextInt();
balance = balance + deposit;
System.out.println("Your Money has been successfully
depsited");
System.out.println("");
break;
case 3:
System.out.println("Balance : "+balance);
System.out.println("");
break;
case 4:
System.exit(0);
}
}
}
}
Output:
C:\Users\hp\3D Objects\Desktop\Java\jdk1.8.0_131\bin>java ATM_Transaction
Automated Teller Machine
Choose 1 for Withdraw
Choose 2 for Deposit
Choose 3 for Check Balance
Choose 4 for EXIT
Choose the operation you want to perform:2
Enter money to be deposited:800
Your Money has been successfullydeposited
Narrowing (Explicit)
• The process of assigning a larger type into a smaller one is called narrowing.
• Casting into a smaller type may result in loss of data.
double long int short byte
For e.g.
class narrowing
{
Public static void main(String[])
{
double d=100.04;
long l=(long) d;
int i=(int) l;
System.out.println(“Int value is”+i);
System.out.println(“Long value is”+l);
System.out.println(“Float value is”
}
}
Ans:
Class:
Class is a set of objects, which shares common characteristics/ behavior and
common properties/ attributes.
Object:
It is a basic unit of Object-Oriented Programming and represents real-life
entities.
Example:
class Student
{
int id;
String name;
public static void main(String args[])
{
Student s1=new Student(); //creating an object of Student
}
}
In this example, we have created a Student class which has two data
members id and name. We are creating the object of the Student s1 by new
keyword.
Ans:
import java.util.Scanner;
class ArmstrongWhile
{
public static void main(String[] arg)
{
int i=0,arm;
System.out.println("Armstrong numbers between 0 to 999");
while(i<1000)
{
arm=armstrongOrNot(i);
if(arm==i)
System.out.println(i);
i++;
}
}
static int armstrongOrNot(int num)
{
int x,a=0;
while(num!=0)
{
x=num%10;
a=a+(x*x*x);
num/=10 ;
}
return a;
}
}
Output:
Armstrong numbers between 0 to 999
0
1
153
370
371
407
Ans:
1. Compile & Interpreted: Java is a two staged system. It combines both
approaches. First java compiler translates source code into byte code
instruction. Byte codes are not machine instructions. In the second
stage java interpreter generates machine code that can be directly
executed by machine. Thus, java is both compile and interpreted
language.
2. Platform independent and portable: Java programs are portable i.e.
it can be easily moved from one computer system to another. Changes
in OS, Processor, system resources won’t force any change in java
programs. Java compiler generates byte code instructions that can be
implemented on any machine as well as the size of primitive data type
is machine independent.
3. Object Oriented: Almost everything in java is in the form of an object.
All program codes and data reside within objects and classes. Like other
OOP languages java also has basic OOP properties such as
encapsulation, polymorphism, data abstraction, inheritance etc. Java
comes with an extensive set of classes (default) in packages.
4. Robust & Secure: Java is robust in the sense that it provides many
safeguards to ensure reliable codes. Java incorporates the concept of
exception handling which captures errors and eliminates any risk of
crashing the system. Java system not only verifies all memory access
but also ensure that no viruses are communicated with an applet. It
does not use pointers by which you can gain access to memory
locations without proper authorization.
5. Distributed: It is designed as a distributed language for creating
applications on the network. It has ability to share both data and
program. Java applications can open and access remote objects on
internet as easily as they can do in local system.
6. Multithreaded: It can handle multiple tasks simultaneously. Java
makes this possible with the feature of multithreading. This means that
we need not wait for the application to finish one task before beginning
another.
7. Dynamic and Extensible: Java is capable of dynamically linking new
class library’s method and object. Java program supports functions
written in other languages such as C, C++ which are called as native
methods. Native methods are linked dynamically at run time.