0% found this document useful (0 votes)
2 views7 pages

Remedial Tutorial 1 Solution

The document provides a remedial tutorial on basic syntactical constructs in Java, covering logical and bitwise operators, ASCII value computation, ATM machine functionality, type conversion, class and object definitions, Armstrong numbers, and features of Java. It includes example programs demonstrating each concept. Key features of Java such as being compile & interpreted, platform independent, object-oriented, robust & secure, distributed, multithreaded, and dynamic are also discussed.

Uploaded by

patilvedant386
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views7 pages

Remedial Tutorial 1 Solution

The document provides a remedial tutorial on basic syntactical constructs in Java, covering logical and bitwise operators, ASCII value computation, ATM machine functionality, type conversion, class and object definitions, Armstrong numbers, and features of Java. It includes example programs demonstrating each concept. Key features of Java such as being compile & interpreted, platform independent, object-oriented, robust & secure, distributed, multithreaded, and dynamic are also discussed.

Uploaded by

patilvedant386
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

REMEDIAL TUTORIAL :- 1 SOLUTION

CHAPTER 01:-BASIC SYNTACTICAL CONSTRUCTS IN JAVA

1) Enlist any two logical operators and two bitwise operators.


Ans:
Logical Operators:

Program demonstrating logical Operators


public class Test
Operat Meanin {
or g public static void main(String args[])
&& Logical {
AND boolean a = true;
|| Logical boolean b = false;
OR System.out.println("a && b = " +
! Logical (a&&b));
NOT System.out.println("a || b = " + (a||b) );
System.out.println("!(a && b) = " + !(a
&& b));
}
}
Output: a && b = false
a || b = true
!(a && b) = true

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:

public class asciivalue


{
public static void main(String args[])
{
// Character whose ASCII is to be computed
char ch = '9';
// Creating a new variable of type int and assigning the character
value.
int ascii = ch;
// Printing the ASCII value of above character
System.out.println("The ASCII value of " + ch+ " is: " + ascii);
}
}
Output:
The ASCII value of 9 is: 57
3) Write a program which displays functioning of ATM machine, (Hint: Withdraw,

Deposit, Check Balance, and Exit)

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

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:1
Enter money to be withdrawn:15000
Insufficient Balance

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:

4) Explain implicit and explicit type conversion with example in detail.


Ans:
In Java, type casting is a method or process that converts a data type into
another data type in both ways manually and automatically. The automatic
conversion is done by the
compiler and manual conversion performed by the programmer.
Type casting is of two types: widening, narrowing.
Widening (Implicit)
• The process of assigning a smaller type to a larger one is known as widening or
implicit.
Byte short int long float double
For e.g.
class widening
{
public static void main(String arg[])
{
int i=100;
long l=I;
float f=l;
System.out.println(“Int value is”+i);
System.out.println(“Long value is”+l);
System.out.println(“Float value is”+f);
}
}

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”
}
}

5) Define the terms with example. i) Class ii) Object

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.

6) Write a program to print all the Armstrong numbers from 0 to 999

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

7) Describe any four features of java.

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.

You might also like