0% found this document useful (0 votes)
56 views21 pages

Java Input and Control Structures

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

Java Input and Control Structures

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

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

accordingly

import [Link];

public class Grading{

public static void main(String[] args){

Scanner sc=new Scanner([Link]);

[Link]("Enter a no: ");

int marks=[Link]();

if(marks>=90) {

[Link]("A grade"); }

else if(marks>=80){

[Link]("B grade"); }

else if(marks>=70){

[Link]("C grade");

else{

[Link]("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++) {
[Link]("*");
}
[Link]();
}
}
}

Output:-
// Use do-while loop to iterate through the first 20 natural
numbers
public class EvenNumbersDoWhile {
public static void main(String[] args) {
[Link]("The even numbers in first 20 natural
numbers are as follows");
int num = 1;
do {
if (num % 2 == 0) {
[Link](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) {
[Link]("The odd numbers in first 20 natural
numbers are as follows");
int num = 1;
while (num <= 20) {
if (num % 2 != 0) {
[Link](num);
}
num++;
}
}
}
Output:-

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


in Java language
import [Link];

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

void acceptDetails(int empID, String empName, String


Designation, int Salary){
[Link] = empID;
[Link] = empName;
[Link] = Designation;
[Link] = Salary;
}

void displayDetails(){
[Link]("Employee ID = "+empID);
[Link]("Employee Name = "+empName );
[Link]("Employee Designation = "+Designation);
[Link]("Employee Salary = "+Salary);
}
}

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

[Link]("Enter employee ID: ");


int empID = [Link]();
[Link]("Enter employee name: ");
String empName = [Link]();
[Link]();
[Link]("Enter employee designation: ");
String Designation = [Link]();
[Link]("Enter employee salary: ");
int Salary = [Link]();
Employee emp1 = new Employee();
[Link](empID, empName, Designation, Salary);
[Link]();
}
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) {
[Link]("Addition of two numbers: " + (a
+ b));
}

// Constructor 2: Adds three numbers


Addition(int a, int b, int c) {
[Link]("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{
[Link]("Adition of the numbers are");
void add(int a, int b){
[Link](a+b);
}
void add(int a, int b, int c){
[Link](a+b+c);
}
}

class MainClass{
public static void main (String[] args){
Addition a1=new Addition();
[Link](5,9);
[Link](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<[Link]; i++){


[Link](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=[Link]();
[Link](pldrm);

[Link]("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= [Link](str2+str3);

[Link](str1);
[Link](str2);
[Link](str3);
[Link]("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(){
[Link]("An ANIMAL IS CREATED");
}
public void eat(){
[Link]("THE ANIMAL EATS");
}
}

class Dog extends Animal{


public Dog(){
super(); //use of super keyword
[Link]("A DOG IS CREATED");

}
@Override
public void eat(){
[Link]();
[Link]("THE DOGS EATS DOG FOOD");
}
}

public class Main{


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

Conclusion:- Hence, we have studied inheritence and


how to applied in programming.
import [Link];
abstract class Shape {
double radius;
Shape(double radius) {
[Link] = radius;
}
abstract double calculateVolume();
}

class Sphere extends Shape {

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

@Override
double calculateVolume() {
return (4.0 / 3.0) * [Link] * [Link](radius, 3);
}
}

class Hemisphere extends Shape {

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

[Link]("Enter the radius: ");


double radius = [Link]();

Shape sphere = new Sphere(radius);


Shape hemisphere = new Hemisphere(radius);

[Link]("Volume of the sphere: %.2f\n",


[Link]());
[Link]("Volume of the hemisphere: %.2f\n",
[Link]());
[Link]();
}
}
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() {
[Link]("Hello");
}
}

public class HelloWorld {

public static void main(String[] args) {

Greet greet = new Hello();

[Link]();
}
}
output:-

Conclusion:- Hence, we have studied how to


implement interface and get desired output

You might also like