Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
29 views

Java 1 To 10 Practicals

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views

Java 1 To 10 Practicals

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 21

//Getting input marks form user by implementing scanner class and giving grade

accordingly

import java.util.Scanner;

public class Grading{

public static void main(String[] args){

Scanner sc=new Scanner(System.in);

System.out.println("Enter a no: ");

int marks=sc.nextInt();

if(marks>=90) {

System.out.println("A grade"); }

else if(marks>=80){

System.out.println("B grade"); }

else if(marks>=70){

System.out.println("C grade");

else{

System.out.println("D grade");

}}

}
Output:-
Conclusion:- Hence, we have studied how to accept input form user in
Java language
//Implement for loop to get following pattern
public class StarPatternForLoop {
public static void main(String[] args) {
for (int i = 1; i <= 5; i++) {
for (int j = 1; j <= i; j++) {
System.out.print("*");
}
System.out.println();
}
}
}

Output:-
// Use do-while loop to iterate through the first 20 natural
numbers
public class EvenNumbersDoWhile {
public static void main(String[] args) {
System.out.println("The even numbers in first 20 natural
numbers are as follows");
int num = 1;
do {
if (num % 2 == 0) {
System.out.println(num);
}
num++;
} while (num <= 20); }
}

Output:-
// Use a while loop to iterate through the first 20 natural
numbers
public class OddNumbersWhileLoop {
public static void main(String[] args) {
System.out.println("The odd numbers in first 20 natural
numbers are as follows");
int num = 1;
while (num <= 20) {
if (num % 2 != 0) {
System.out.println(num);
}
num++;
}
}
}
Output:-

Conclusion:- Hence, we have studied how to implement various loops


in Java language
import java.util.Scanner;

class Employee{
int empID;
String empName;
String Designation;
int Salary;

void acceptDetails(int empID, String empName, String


Designation, int Salary){
this.empID = empID;
this.empName = empName;
this.Designation = Designation;
this.Salary = Salary;
}

void displayDetails(){
System.out.println("Employee ID = "+empID);
System.out.println("Employee Name = "+empName );
System.out.println("Employee Designation = "+Designation);
System.out.println("Employee Salary = "+Salary);
}
}

}
class empDetails{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);

System.out.println("Enter employee ID: ");


int empID = sc.nextInt();
System.out.println("Enter employee name: ");
String empName = sc.nextLine();
sc.nextLine();
System.out.println("Enter employee designation: ");
String Designation = sc.nextLine();
System.out.println("Enter employee salary: ");
int Salary = sc.nextInt();
Employee emp1 = new Employee();
emp1.acceptDetails(empID, empName, Designation, Salary);
emp1.displayDetails();
}
Output:-

Conclusion:- Hence, we have studied how to use classes and object


and have successfully demonstrated it
class Addition {
// Constructor 1: Adds two numbers
Addition(int a, int b) {
System.out.println("Addition of two numbers: " + (a
+ b));
}

// Constructor 2: Adds three numbers


Addition(int a, int b, int c) {
System.out.println("Addition of three numbers: " +
(a + b + c));
}
}

class MainClass {
public static void main(String[] args) {
// Using Constructor 1 to add two numbers
Addition addTwo = new Addition(5, 9);

// Using Constructor 2 to add three numbers


Addition addThree = new Addition(7, 6, 9);
}
}
Output:-

Conclusion:- Hence, we have studied how to implement constructior


overloading
class Addition{
System.out.println("Adition of the numbers are");
void add(int a, int b){
System.out.println(a+b);
}
void add(int a, int b, int c){
System.out.println(a+b+c);
}
}

class MainClass{
public static void main (String[] args){
Addition a1=new Addition();
a1.add(5,9);
a1.add(7,6,9);
}
}
output:-

Conclusion:- Hence, we have studied how to implement method


overloading
class Mainclass{
public static void main(String [] args){
int [] arr= new int[6];
arr[0] = 10;
arr[1] = 20;
arr[2] = 30;
arr[3] = 40;
arr[4]= 50;
arr[5] = 60;

for(int i=0; i<arr.length; i++){


System.out.println(arr[i]);
}
}

}
Output:-

Conclusion:- Hence, we have studied how to implement array


and how to print the elements of the array
//Lenth of the string
class StringDemo{
public static void main(String[]args){
String pldrm="Able was I ere I saw Elba";
int len=pldrm.length();
System.out.println(pldrm);

System.out.println("Length of the string is "+len);


}
}

output:-
//concatation of strings
class StringDemo{
public static void main(String[]args){

String str1="Kajol";
String str2="Raju";
String str3="Tarate";
String str4= str1.concat(str2+str3);

System.out.println(str1);
System.out.println(str2);
System.out.println(str3);
System.out.println("Concatation of above string is "+str4);
}
}

Output:-

Conclusion:- Hence, we have studied how to use classes and object


and have successfully demonstrated it
class Animal{
public Animal(){
System.out.println("An ANIMAL IS CREATED");
}
public void eat(){
System.out.println("THE ANIMAL EATS");
}
}

class Dog extends Animal{


public Dog(){
super(); //use of super keyword
System.out.println("A DOG IS CREATED");

}
@Override
public void eat(){
super.eat();
System.out.println("THE DOGS EATS DOG FOOD");
}
}

public class Main{


public static void main(String [] args){
Dog myDog = new Dog();
myDog.eat();
}
}
output:-

Conclusion:- Hence, we have studied inheritence and


how to applied in programming.
import java.util.Scanner;
abstract class Shape {
double radius;
Shape(double radius) {
this.radius = radius;
}
abstract double calculateVolume();
}

class Sphere extends Shape {

Sphere(double radius) {
super(radius);
}

@Override
double calculateVolume() {
return (4.0 / 3.0) * Math.PI * Math.pow(radius, 3);
}
}

class Hemisphere extends Shape {

Hemisphere(double radius) {
super(radius);
}
@Override
double calculateVolume() {
return (2.0 / 3.0) * Math.PI * Math.pow(radius, 3);
}
}
public class VolumeCalculator {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);

System.out.print("Enter the radius: ");


double radius = scanner.nextDouble();

Shape sphere = new Sphere(radius);


Shape hemisphere = new Hemisphere(radius);

System.out.printf("Volume of the sphere: %.2f\n",


sphere.calculateVolume());
System.out.printf("Volume of the hemisphere: %.2f\n",
hemisphere.calculateVolume());
scanner.close();
}
}
Output:-

Conclusion:- Hence, we have studied how


to implement abstraction through
abstract class and found out volume of
hemisphere and sphere by radius of user
input.
interface Greet {

void sayHello();
}

class Hello implements Greet {

@Override
public void sayHello() {
System.out.println("Hello");
}
}

public class HelloWorld {

public static void main(String[] args) {

Greet greet = new Hello();

greet.sayHello();
}
}
output:-

Conclusion:- Hence, we have studied how to


implement interface and get desired output

You might also like