Control Statements and Operators

Today was about operators and control statements

 

Operators:

  1. Arithmatic : +,-,*,/,%,++,–,+=,-=,/=,*=,%=
  2. Relational: >,<,>=,<=,=,!=
  3. Bitwise
  4. Boolean Logical

 

Control Statements

  1. Selection statements
  • If else
  • Switch
  1. Iteration
  • while
  • do while
  • for
  1. Jump
  • break
  • continue

import java.util.Scanner;

 

class Ss

{

public static void main(String s[])

{

System.out.println(“Enter a number from 1-7”);

Scanner r= new Scanner(System.in);

int d=r.nextInt();

do

{

System.out.println(“Yes”);

d++;

} while(d<7);

}

}

 

 

 

 

class Sel

{

public static void main(String s[])

{

if(s[0].equals(“0”))

{

System.out.println(“VOTER AGE IS VALID”);

}

else

{

System.out.println(“VOTER AGE IS INVALID”);

}

}

}

Some more programs and basic logic

How to get an input from a user:

use of function ShowInputDialog(“”). but it shows error. We have to include JOptionpane.ShowInputDialog(“”). it still gives error. so we include header library import javax.swing.*;

import java.util.Scanner;

class Ss
{
public static void main(String s[])
{
System.out.println(“Enter a number from 1-7”);
Scanner r= new Scanner(System.in);
int d=r.nextInt();
do
{
System.out.println(“Yes”);
d++;
} while(d<7);
}
}

How to convert a string value to integer value

Integer.ParseInt(“the string that has to be converted into integer value”)

Use of loops

  1. While
  2. for
  3. do-while
  4. foreach

Each loop needs initialization, condition, increment/decrement.

Assignments

1. WAP for sum of all digits.

class sum

{

public static void main(String[] args)

{ int sum=0;

while(n!=0)

{

sum=n%10;

n%=10;

}

System.out.print(“sum of all digits:”+sum);

 

 

2.WAP to sum of first and last digit,

class sum

{

public static void main(String[] args)

{

int no,last_digit,first_digit,total;

no=145;

last_digit=n%10;

total=last_digit;

first_digit=(no/1000)%10;

total=total+first digit;

System.out.print(” Sum :” +total);

}

 

 

3. WAP for cash denomination.

class sum

{

h=amt/100;

amt=amt%100;

f=amt/50;

amt=amt%50;

ten=amt/20;

amt=amt%20;

ten=amt/10;

System.out.print(“cash denomination is :” +amt);

 

}

Simple Programs

  1. Program to swap two numbers
  2. Program to reverse a 3 digit number
  3. Program to find sum of first and last digit of a number
  4. Program to find sum of all digits of a number
  5. Program for cash denomination

Use of loops like

  1. if-else loop
  2. ladder if-else
  3. nested if-else

Introduction about armstrong and palindrome numbers.

1. WAP to Swap 2 numbers.

Class add

{ public static void main(String [] args)

int a,b,c;

a=3;

b=8;

a=b;

b=c;

c=a;

System.out.print(a);

System.out,print(b);

}

 

2.WAP to Swap 2 no.s using third variable.

Class add

{ public static void main(String [] args)

int a,b,c;

a=3;

b=8;

a=a+b;

b=a-b;

a=a-b;

System.out.print(a);

System.out,print(b);

}

 

3.WAP to reverse a 3-digit no.

Class add2

{ public static void main(String [] args)

int a,b=0;

a=321;

while(a!=0)

{

b=b*10;

b=b*10+a%10;

a=a/10;

System.out.print(“the reversed no. is : “+a”);

}

Data types and conversions

Today we learnt about various datatypes- Integer , floating points, Characters and boolean

Integer further has subtypes

  • Byte-1 Byte
  • Short-2 Bytes
  • Int-4 Bytes
  • Long-8 Bytes

Floating Points

  • Float- 4 Bytes
  • Double- 8 Bytes

Characters

  • Char
  • String

 

I also learnt about promotion of expressions. Automatic promotion of expressions is done by the compiler.

 

Small Datatype————–> Bigger Datatype

done by the compiler.

Types of conversions

  • Implicit-automatic
  • Explicit-manual

 

Basics of Java

Today’s class was mostly an extension of yesterday’s class as we understood the concept of classes better, the flow of a java program. We discussed various errors some of us faced the previous day. With each error our instructor told us its cause and how to avoid it.

We were also told that java comes as Java SE, EE and ME. Since some of us were using Ubuntu so we also learnt how to install JDK on Ubuntu.

We were briefed about IDEs. What they are? What they do? How they make programming simpler. However we were also told that we would not be using any IDE for the beginning, instead we would be using notepad so as to learn better. The IDEs most popularly used by java developers are eclipse and netbeans. I have already worked with netbeans so I was looking forward to work with eclipse

Starting with Java

Today was my first day as a trainee at Centre for Development of Advanced Computing. Its a beautiful campus , well equipped and also strict in terms of its rules and regulations. Today the first class was basically about introduction , our introduction, the institute, the teacher as well as the course we were going to pursue that is Java.

Java was a turnaround for the world of internet especially, because of its platform independence and portability. Java is one of the most widely used languages in the world. I also understood its structure, the classes, functions etc , the elements through which it behaves.

I also understood what makes java platform independent and how it has changed the face of software over the years.

After this introductory class we wrote a few simple programs to print various questions and had a question answer session where are doubts were cleared.

Thats the end of day 1. It was a good start. Looking forward to a learning experience.