Commit 57e55739 authored by Arsam Ali's avatar Arsam Ali

new practice code

parent 590df624
package AccessModifiers;
class Student {
int rollNo = 5;
void showRollNo(){
System.out.println("RollNo = " + rollNo);
}
public static void main(String args[]){
Student obj = new Student();
System.out.println(obj.rollNo);
obj.showRollNo();
}
}
package AccessModifiers;
class Private {
private String name;
public String getName() {
return this.name;
}
public void setName(String name) {
this.name= name;
}
public static void main(String[] main){
Private d = new Private();
// access private variable and field from another class
// d.name = "Programiz"; // shows error we have to use getter setter for it
// access the private variable using the getter and setter
d.setName("Programiz");
System.out.println(d.getName());
}
}
package AccessModifiers;
class Protected {
protected void display() {
System.out.println("I am an animal");
}
}
class Dog extends Protected {
public static void main(String[] args) {
Dog dog = new Dog();
dog.display();
}
}
package AccessModifiers;
// Animal.java file
// public class
public class Public {
public int legCount;
public void display() {
System.out.println("I am an animal.");
System.out.println("I have " + legCount + " legs.");
}
public static void main( String[] args ) {
Public animal = new Public();
animal.legCount = 4;
animal.display();
}
}
package ConditionalOperators;
public class PracticeConditionalOperators {
public static void main(String[] args) {
String out;
int a = 6, b = 12;
out = a==b ? "Yes":"No";
System.out.println("Ans: "+out);
//Second Example
int x=5, y=4, z=7;
System.out.println(x>y && x>z || y<z);
System.out.println((x<z || y>z) && x<y);
//third Example
int T, L;
T = 20;
L = (T == 1) ? 61: 90;
System.out.println("Value of y is: " + L);
L = (T == 20) ? 61: 90;
System.out.println("Value of y is: " + L);
}
}
package Constructors;
public class Nonparametrizedconstructors {
private String name;
Nonparametrizedconstructors() {
System.out.println("Constructor Called:");
name = "Hello This is for Bi-weekly-Practice";
}
public static void main(String[] args) {
Nonparametrizedconstructors obj = new Nonparametrizedconstructors();
System.out.println("The name is " + obj.name);
}
}
package Constructors;
public class ParametrizedConstructor {
String studentName;
ParametrizedConstructor(String name){
studentName = name;
}
ParametrizedConstructor(ParametrizedConstructor myObj){
this.studentName = myObj.studentName;
}
void display(){
System.out.println("Student:" + studentName);
}
public static void main(String args[])
{
ParametrizedConstructor obj1 = new ParametrizedConstructor("Arsam");
ParametrizedConstructor obj2 = new ParametrizedConstructor(obj1);
System.out.println("Printing obj1 - ");
obj1.display();
System.out.println("Printing obj2 - ");
obj2.display();
}
}
package Constructors;
public class SecondExampleConstructor {
String studentName;
int studentAge;
String member;
SecondExampleConstructor(){
member = "YES";
}
SecondExampleConstructor(String name , int age){
this();
studentName = name;
studentAge = age;
}
void display(){
System.out.println(studentName + " -" + studentAge+ "-"+ "Member" + member);
}
public static void main(String args[])
{
SecondExampleConstructor obj = new SecondExampleConstructor("Arsam" , 21);
obj.display();
}
}
package DecisionMaking;
import java.util.Scanner;
import java.util.*;
public class IfStatement {
public static void main(String args[]){
int a=20, b=30;
if(b>a)
System.out.println("b is greater");
}
}
class Oddeven
{
public static void main(String[] args)
{
int no;
Scanner s=new Scanner(System.in);
System.out.println("Enter any number :");
no=s.nextInt();
if(no%2==0)
{
System.out.println("Even number");
}
else
{
System.out.println("Odd number");
}
}
}
class switchCase
{
public static void main(String arg[])
{
int ch;
System.out.println("Enter any number (1 to 7) :");
Scanner s=new Scanner(System.in);
ch=s.nextInt();
switch(ch)
{
case 1:
System.out.println("Today is Monday");
break;
case 2:
System.out.println("Today is Tuesday");
break;
case 3:
System.out.println("Today is Wednesday");
break;
case 4:
System.out.println("Today is Thursday");
break;
case 5:
System.out.println("Today is Friday");
break;
case 6:
System.out.println("Today is Saturday");
break;
case 7:
System.out.println("Today is Sunday");
default:
System.out.println("Only enter value 1 to 7");
}
}
}
package DecisionMaking;
public class LeapYearExample {
public static void main(String args[]) {
int year = 2020;
if (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0)) {
System.out.println("LEAP YEAR");
} else {
System.out.println("COMMON YEAR");
}
}
}
package Filling;
import java.util.Arrays;
import java.util.Scanner;
public class FillingExamples {
public static void main(String[] args)
{
int size, element;
Scanner sc = new Scanner(System.in);
System.out.print("Enter the size of the array: ");
size = sc.nextInt();
int[] array = new int[100];
System.out.println("Enter the elements in the array: ");
for(int i = 0; i < size; i++)
{
array[i] = sc.nextInt();
}
System.out.print("Enter the element which you want to fill in the array: ");
element = sc.nextInt();
Arrays.fill(array, element);
System.out.println("After filling" +element+ " in complete array:\n" + Arrays.toString(array));
}
//Second example of Filling
{
int size, element, index1, index2;
Scanner sc = new Scanner(System.in);
System.out.print("Enter the size of the array: ");
size = sc.nextInt();
int[] array = new int[100];
System.out.println("Enter the elements in the array: ");
for(int i = 0; i < size; i++)
{
array[i] = sc.nextInt();
}
System.out.print("Enter the element which you want to fill in the array: ");
element = sc.nextInt();
System.out.print("From which index you want to fill "+element+" in the array: ");
index1 = sc.nextInt();
System.out.print("To which index you want to fill "+element+" in the array: ");
index2 = sc.nextInt();
Arrays.fill(array, index1, index2, element);
System.out.println("After filling" +element+ " in the array:\n" + Arrays.toString(array));
}
}
package Oopconcepts.Abstraction;
abstract class Animal {
public abstract void animalSound();
public void sleep() {
System.out.println("Zzz");
}
}
class Cat extends Animal {
public void animalSound() {
System.out.println("The cat says: Meowwn");
}
}
class Main {
public static void main(String[] args) {
Cat myPig = new Cat();
myPig.animalSound();
myPig.sleep();
}
}
package Oopconcepts.Abstraction;
public class Car {
//data members
private int speed;
private String color;
//getter setters of above data members.
public int getSpeed() {
return speed;
}
public void setSpeed(int speed) {
this.speed = speed;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
}
package Oopconcepts.Encupsulation;
import Oopconcepts.Abstraction.Car;
class CarTest {
public static void main(String args[]){
//create Car class object
Car car1 = new Car();
//set car details.
car1.setColor("white");
car1.setSpeed(120);
//get and print car details.
System.out.println("Car color: " + car1.getColor());
System.out.println("Car speed: " + car1.getSpeed());
}
}
package Oopconcepts.Inheritance;
class Vehicle {
protected String brand = "Ford";
public void honk() {
System.out.println("Tuut, tuut!");
}
}
class Car extends Vehicle {
private final String modelName = "Mustang";
public static void main(String[] args) {
Car myCar = new Car();
myCar.honk();
System.out.println(myCar.brand + " " + myCar.modelName);
}
}
\ No newline at end of file
package Oopconcepts.Polymorphism;
class Animal {
public void animalSound() {
System.out.println("The animal makes a sound");
}
}
class cat extends Animal {
public void animalSound() {
System.out.println("The cat says: mewee mewee");
}
}
class Dog extends Animal {
public void animalSound() {
System.out.println("The dog says: bow wow");
}
}
class Main {
public static void main(String[] args) {
Animal myAnimal = new Animal();
Animal myCat = new cat();
Animal myDog = new Dog();
myAnimal.animalSound();
myCat.animalSound();
myDog.animalSound();
}
}
\ No newline at end of file
package StringMethods;
public class StringSplit {
public static void main(String[] args) {
String text = "Arsam Ali Rajput";
String[] result = text.split(" ");
System.out.print("result = ");
for (String str : result) {
System.out.print(str + ", ");
}
}
}
package StringMethods;
public class StringSubSequence {
public static void main(String[] args) {
String str = "Karachi jami commercial";
System.out.println(str.subSequence(3, 8));
}
}
package StringMethods;
public class StringcompareTo {
public static void main(String[] args) {
String str1 = "Arsam Ali";
String str2 = "Hamza Ali";
int result;
result = str1.compareTo(str2);
System.out.println(result);
}
}
package StringMethods;
public class StringcompareToIgnoreCase {
public static void main(String[] args) {
String str1 = "Arsam";
String str2 = "Rahim";
String str3 = "Umer";
int result;
result = str1.compareToIgnoreCase(str2);
System.out.println(result);
result = str1.compareToIgnoreCase(str3);
System.out.println(result);
result = str3.compareToIgnoreCase(str1);
System.out.println(result);
}
}
package StringMethods;
public class Stringlength {
public static void main(String[] args) {
String str1 = "nisum pakistan";
int length = str1.length();
System.out.println(str1.length());
}
}
package StringMethods;
public class StringtoCharArray {
public static void main(String[] args) {
String str = "Nisum icon building";
char[] result;
result = str.toCharArray();
System.out.println(result);
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment