Sdet 1

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 10

SDET – JAVA TRAININGS

1/2/2023 – Day 1
1)To print Hello World:

package com.basicSamples;

public class HelloWorld {


public static void main(String[] args) {
System.out.println("Hello World...");

}
}

2) Calculator
package com.basicSamples;
public class Calculator {
int number; //Instance Variable
static int c; // Static Variable
public void add(int a, int b){
int sum; // Local Variable
sum=a+b;
System.out.println(sum);
}
public static void main(String[] args) {
Calculator cobj=new Calculator();
cobj.add(10,25);
cobj.number=10;
c=15;

}
}

3) Data Types:
package com.basicSamples;

public class DataTypeSample {

public static void main(String[] args) {


//double, float
double myDouble = 3.4;
float myFloat=3.4f;
double myDoubleScientific = 3.445e2;
System.out.println(myDouble);
System.out.println(myFloat);
System.out.println(myDoubleScientific);

//char
char letter='a';
System.out.println(letter);

//string
String str1="Java Programming";
System.out.println(str1);
String str2="programiz";
System.out.println(str2);

//binary
int binaryNumber=0b10010;
System.out.println(binaryNumber);

//octal
int octalNumber=027;
System.out.println(octalNumber);

//decimal
int decNumber=34;
System.out.println(decNumber);

//hexadecimal
int hexNumber=0x2F;
System.out.println(hexNumber);

//Byte range
byte range;
range=124;
System.out.println(range);

//short type
short temperature;
temperature=-200;
System.out.println(temperature);

int irange=-4250000;
System.out.println(irange);

//long
long lrange=-4324569780000L;
System.out.println(lrange);
}
}
4) Operators

package com.basicSamples;

public class OperatorsSample {

public static void main(String[] args) {


//declare variables
int a=12, b=5;

//addition operator
System.out.println("a+b = "+(a+b));

//subtraction operator
System.out.println("a-b = "+(a-b));

//multiplication operator
System.out.println("a*b = "+(a*b));

//division operator
System.out.println("a/b = "+(a/b));

//modulo operator
System.out.println("a%b = "+(a%b));

//create variables
int ac=4;
int var;

//assign value using =


var =ac;
System.out.println("var using =: "+var);

//assign value using =+


var +=ac;
System.out.println("var using +=: "+var);

//assign value using =*


var *=ac;
System.out.println("var using *=: "+var);

}
}

2/2/2023 – Day 2

5) Relational Operator
package com.basicSamples;

public class Relational_Operator {

public static void main(String[] args) {

int a = 7, b = 11;
// value of a and b
System.out.println("a is " + a + " and b is " + b);
// == operator
System.out.println(a == b);
// != operator
System.out.println(a != b);
// > operator
System.out.println(a > b);
// < operator
System.out.println(a < b);
// >= operator
System.out.println(a >= b);
// <= operator
System.out.println(a <= b);
// && operator
System.out.println((5 > 3) && (8 > 5));
System.out.println((5 > 3) && (8 < 5));
// || operator
System.out.println((5 < 3) || (8 > 5));
//System.out.println((5 > 3) || (8 < 5));
System.out.println((5 < 3) || (8 < 5));
// ! operator
System.out.println(!(5 == 3));
System.out.println(!(5 > 3));

}
}

6)Unary Operator
package com.basicSamples;

public class Unary_Operator {

public static void main(String[] args) {


int a=12, b=12;
int result1,result3;
System.out.println("A value : "+ a);
//increment
result1=++a;
int result2=a++;
System.out.println("After pre Increment: "+ result1);
System.out.println("After post Increment: "+ result2);

System.out.println("Value of b: "+ b);


//decrement
result3=--b;
int result4=b++;
System.out.println("After pre Decrement: "+result3);
System.out.println("After post Decrement: "+result4);

}
7)Ternary Operators
package com.basicSamples;

public class Ternary_Operator {

public static void main(String[] args) {


// ternary operator
int februaryDays =29;
String result=februaryDays==28?"Not a leap year":"Leap year";
System.out.println(result);
}
}

8)ConsoleInSample
package com.basicSamples;

import java.util.Scanner;

public class ConsoleInSample {

public static void main(String[] args) {


Scanner sc=new Scanner(System.in);
//integer
System.out.println("Enter an integer: ");
int n=sc.nextInt();
System.out.println("Entered num " + n);

//float
System.out.println("Enter a foalt");
float f=sc.nextFloat();
System.out.println("Entered num " + f);

//double
System.out.println("Enter a double: ");
double d=sc.nextDouble();
System.out.println("Entered num: " + d);

//String
System.out.println("Enter a string: ");
String s =sc.next();
System.out.println("Entered string : "+s);

//long
System.out.println("Enter a long: ");
Long l =sc.nextLong();
System.out.println("Entered long : "+l);
sc.close();
}
}

9) Kid Class
package com.ClassesAndObjects;

public class Kid {


String name;
int age;
public Kid(String name, int age) {
this.name=name;
this.age=age;
}
public void play() {
System.out.println(this.name + " is playing..");

Kid Driver Class


package com.ClassesAndObjects;

public class KidDriver {

public static void main(String[] args) {


Kid kidOne = new Kid("pinky", 8);
kidOne.play();
Kid kidTwo = new Kid("Vicky", 8);
//System.out.println(kidTwo.name);
kidTwo.play();
}
}
OtherDriver
package com.basicSamples;

import com.ClassesAndObjects.Kid;

public class OtherDriver {

public static void main(String[] args) {


Kid kid=new Kid("John",10);
System.out.println(kid);

}
EncapsulateKid

package com.ClassesAndObjects;
public class EncapsulatedKid {
private String getName() {
return name;
}
public void setName(String name) {
//this.name = name;
this.name = (name.length()==0)?"No name": name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age=(age>1 && age<13)? age: 2;
}

public void play() {


System.out.println(this.name +" is playing..");
}
private String name;
private int age;
public void play() {
System.out.println(this.name +" is playing..");
}

3/3/2023-Day 3
1) Method Overloading
package com.ClassesAndObjects;

public class Kid {


String name;
int age;
public void printKid(String name) {
System.out.println("Name of the kid is : "+this.name+"andAge is:
"+this.age);
}
public void printKid (int studying) {
System.out.println("kid is studying in :"+studying);
}
public void printKid (String name, int age) {

System.out.println("kid name is : "+name);


}
}
Public static void main(String[]args){
Kid kidTwo = new Kid("Vicky", 8);
//System.out.println(kidTwo.name);
kidTwo.play();
kidTwo.printKid("Pinky", 20);
kidTwo.printKid(10);
}
}

2) Final Keyword

package com.basicSamples;
public class Calculator {
final double pi=3.14;
public void area(double radius) {
//pi=5.23;
double area=pi*radius*radius;
System.out.println("Area is "+ area);
}

public static void main(String[] args) {


Calculator a1=new Calculator();
a1.area(34.67);
}
}

3) String

package com.ClassesAndObjects;
public class StringSample {

public static void main(String[] args) {

//create a strings
String type="Java Programming";
String first="Java is a Object oriented Programming ";
String second=" Python";
String third=" Language";

//print strings
System.out.println(first);
System.out.println(second);
System.out.println(third);

//Length of the string


System.out.println("Length of the string : "+first.length());

//concat two strings


String joinedString = first.concat(third);
System.out.println("Joined String is "+joinedString);

//compare two strings


first ="Java Programming";
second="Java Programming";
third="Python Programming";

//compare first and second strings


boolean result1=first.equals(second);
System.out.println("string first and second are equal: "+result1);

//compare first and third strings


boolean result2=first.equals(third);
System.out.println("string first and third are equal: "+result2);

String example="Hello";
example = example.concat("Welcome");
}
}

You might also like