0% found this document useful (0 votes)
24 views8 pages

Program For Switch Statement

The document contains 12 code examples demonstrating different concepts in Java including switch statements, constructors, destructors, and more. Example 1-2 show use of switch statements. Examples 3-6 demonstrate break and continue statements. Examples 7-11 showcase different types of constructors like no-arg, parameterized, default, copy. The final example 12 shows how to create a destructor method.

Uploaded by

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

Program For Switch Statement

The document contains 12 code examples demonstrating different concepts in Java including switch statements, constructors, destructors, and more. Example 1-2 show use of switch statements. Examples 3-6 demonstrate break and continue statements. Examples 7-11 showcase different types of constructors like no-arg, parameterized, default, copy. The final example 12 shows how to create a destructor method.

Uploaded by

Friel Mado
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

1.

Program for switch statement

public class program11

public static void main(String[] args)

int number = 48;

String size;

// switch statement to check size

switch (number)

case 29:

size = "Small";

break;

case 42:

size = "Medium";

break;

// match the value of week

case 44:

size = "Large";

break;

case 48:

size = "Extra Large";

break;

default:

size = "Unknown";

break;

System.out.println("Size: " + size);

} }
2. Program using switch statement to display based on the number

public class program21 {

public static void main(String[] args) {

//Declaring a variable for switch expression

int number=20;

//Switch expression

switch(number){

//Case statements

case 10: System.out.println("10");

break;

case 20: System.out.println("20");

break;

case 30: System.out.println("30");

break;

//Default case statement

default:System.out.println("Not in 10, 20 or 30");

3. Program to display the numbers using break statement

public class program12 {

public static void main(String[] args) {

// for loop

for (int i = 1; i <= 10; ++i) {

// if the value of i is 5 the loop terminates

if (i == 6) {
break;

System.out.println(i);

4. Program to display the sum of positive numbers using break statement

import java.util.Scanner;

public class program13 {

public static void main(String[] args) {

Double number, sum = 0.0;

// create an object of Scanner

Scanner input = new Scanner(System.in);

while (true) {

System.out.print("Enter a number: ");

// takes double input from user

number = input.nextDouble();

// if number is negative the loop terminates

if (number < 0.0) {

break;

sum += number;

System.out.println("Sum = " + sum);

}
5. Program to display the numbers using continue statement

public class program14{

public static void main(String[] args) {

// for loop

for (int i = 1; i <= 10; ++i) {

// if value of i is between 4 and 9

// continue is executed

if (i > 4 && i < 9) {

continue;

System.out.println(i);

6. Program to display the numbers using continue statement

public class program20 {

public static void main(String[] args) {

//for loop

for(int i=1;i<=10;i++){

if(i==5){

//using continue statement

continue;//it will skip the rest statement

System.out.println(i);

}
7. Program for No argument constructor

public class program15 {


private String name;

// constructor
program15() {
System.out.println("Constructor Called:");
name = "WELCOME TO JAVA";
}

public static void main(String[] args) {


// constructor is invoked while
// creating an object of the Main class
program15 obj = new program15();
System.out.println("The name is " + obj.name);
}
}

8. Program for parameterized constructor

public class program16 {

String languages;

// constructor accepting single value

program16(String lang) {

languages = lang;

System.out.println(languages + " Programming Language");

public static void main(String[] args) {

// call constructor by passing a single value

program16 obj1 = new program16("Java");

program16 obj2 = new program16("Python");

program16 obj3 = new program16("C");

}
9. Program for default constructor

public class program17 {

int a;

boolean b;

public static void main(String[] args) {

// A default constructor is called

program17 obj = new program17();

System.out.println("Default Value:");

System.out.println("a = " + obj.a);

System.out.println("b = " + obj.b);

10. Program for copy constructor

public class Fruit {

private double fprice;

private String fname;

//constructor to initialize fruit price and name

Fruit(double fPrice, String fName)

fprice = fPrice;

fname = fName;

//creating a copy constructor

Fruit(Fruit fruit)

System.out.println("\nAfter invoking the Copy Constructor:\n");

fprice = fruit.fprice;

fname = fruit.fname;

}
//creating a method that returns the price of the fruit

double showPrice()

return fprice;

//creating a method that returns the name of the fruit

String showName()

return fname;

//class to create student object and print roll number and name of the student

public static void main(String args[])

Fruit f1 = new Fruit(399, "Ruby Roman Grapes");

System.out.println("Name of the first fruit: "+ f1.showName());

System.out.println("Price of the first fruit: "+ f1.showPrice());

//passing the parameters to the copy constructor

Fruit f2 = new Fruit(f1);

System.out.println("Name of the second fruit: "+ f2.showName());

System.out.println("Price of the second fruit: "+ f2.showPrice());

11. Program to demonstrate constructor overloading

public class program18 {

String language;

// constructor with no parameter

program18() {

this.language = "Java";

}
// constructor with a single parameter

program18(String language) {

this.language = language;

public void getName() {

System.out.println("Programming Langauage: " + this.language);

public static void main(String[] args) {

// call constructor with no parameter

program18 obj1 = new program18();

// call constructor with a single parameter

program18 obj2 = new program18("Python");

obj1.getName();

obj2.getName();

12. Program to demonstrate destructor

public class program22 {

public void finalize() throws Throwable{

System.out.println("Object is destroyed by the Garbage Collector");

public static void main(String[] args) {

program22 test = new program22();

test = null;

System.gc();

You might also like