0% found this document useful (0 votes)
4 views7 pages

Java Programs

The document contains multiple Java code snippets demonstrating various programming concepts such as vowel extraction, leap year determination, string reversal, switch-case statements, classes and objects, method overloading, prime number identification, Fibonacci series generation, and inheritance with access specifiers. Each code snippet is designed to illustrate a specific functionality or concept in Java programming. The document serves as a collection of examples for learning and reference purposes.

Uploaded by

Sindhu A
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)
4 views7 pages

Java Programs

The document contains multiple Java code snippets demonstrating various programming concepts such as vowel extraction, leap year determination, string reversal, switch-case statements, classes and objects, method overloading, prime number identification, Fibonacci series generation, and inheritance with access specifiers. Each code snippet is designed to illustrate a specific functionality or concept in Java programming. The document serves as a collection of examples for learning and reference purposes.

Uploaded by

Sindhu A
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
You are on page 1/ 7

VOWELS :

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner ss = new Scanner(System.in);
System.out.println("enter a string :");
String name = ss.nextLine();
ss.close();
List<Character>vowels = new ArrayList<>();

name = name.toLowerCase();

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


char ch = name.charAt(i);
if(ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u')
{
vowels.add(ch);
}
}
System.out.println(vowels);
}
}
LEAP YEAR:
import java.util.Scanner;
public class OddOrEven {
public static void main(String[] args) {
Scanner ss = new Scanner(System.in);
System.out.println("enter your name :");
String name = ss.nextLine();
System.out.println("enter a year :");
int year = ss.nextInt();
ss.close();
if (year % 4==0 && year %100!=0 || year %400 == 0) {
System.out.println("leap year");
} else {
System.out.println("not a leap year");
}
}
}

VOWELS COUNT:
import java.util.Scanner;
public class Main {
public static void main(String args[]){
Scanner ss = new Scanner(System.in);
System.out.println("enter a string :");
String name = ss.nextLine();
ss.close();
name = name.toLowerCase();
int count = 0;
for (int i = 0;i<name.length();i++){
char ch = name.charAt(i);
if(ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u')
{
count ++;
}
}
System.out.println(count);
}
}

IF ELSE :
class HelloWorld {
public static void main(String[] args) {
int mark = 18;
if (mark >=100){
System.out.println("grade O");
}
else if(mark >=90 && mark>=80){
System.out.println("grade A+");
}
else if(mark >=70 && mark>=60){
System.out.println("grade A");
}
else if(mark >=70 && mark>=60){
System.out.println("grade B");
}
else{
System.out.println("fail");

}
}
}
RESERSE A STRING:

public class Main {


public static void main(String[] args) {
String originalStr = "Hello";
String reversedStr = "";
System.out.println("Original string: " + originalStr);

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


reversedStr = originalStr.charAt(i) + reversedStr;
}

System.out.println("Reversed string: "+ reversedStr);


}
}

SWITCH CASE:
class HelloWorld {
public static void main(String[] args) {
int month = 4;
switch (month){
case 1:
System.out.println("jan");
break;
case 2 :
System.out.println("feb");
break;
case 3 :
System.out.println("march");
break;
default:
System.out.println("invalid");
break;

}
}}
CLASSS :

class Student {
int id = 78;
String name="vaidhegi";
public static void main(String args[])
{
Student s1 = new Student();
System.out.println(s1.id);
System.out.println(s1.name);
}
}
OBJECT :

public class car {


private String color:
private String model;
private int year;

public car (String color,String model,int year){


this.color = color;
this.model=model;
this.year= year;
}
public String getcolor(){
return ;
}
public String getmodel(){
return;
}
public int getyear(){
return;
}
public void String setcolor (){
this.color = color;
}
public void String setmodel(){
this.color = color;
}
public void int setyear(){
this.color = color;
}
public void displayeDtails(){
System.out.println(color)
}
}
main :
public class main{
public static void main (String args[]){
car Mycar = new car("blue","hcbj",2002)
Mycar.displayDetails();

}
}

METHOD OVERLOADING:
class Main{
static int add(int a,int b){
return a+b;
}
static double multi(int a, int b){
return a*b;
}
}
public class Example{
public static void main (String args[]){
System.out.println(Main.add(2,3));

}
}

PRIME NUMBER :
public class PrimeNumbers {

public static void main(String[] args) {


int n = 100; // Find prime numbers up to 100
System.out.println("Prime numbers up to " + n + " are:");
for (int i = 2; i <= n; i++) {
if (isPrime(i)) {
System.out.print(i + " ");
}
}
}

public static boolean isPrime(int num) {


if (num <= 1) {
return false;
}
for (int i = 2; i <= Math.sqrt(num); i++) {
if (num % i == 0) {
return false;
}
}
return true;
}
}
SERIES OF NUMBER:

public class series{


public static void main (String args[]){
int num = 50;
for(int i=1;i<=num;i++){
System.out.print(i +" ");
}
}}
FIBONACCISERIES :
public class series{
public static void main (String args[]){
int n = 7;
int a=0,b=1;
for(int i = 1;i<=n;i++){
System.out.print(a +" ");
int next=a+b;
a=b;
b=next;
}
}}

FIND UNIQUE VALUE :

import java.io.*;
class Unique {
static void printDistinct(int arr[], int n)
{
for (int i = 0; i < n; i++)
{
int j;
for (j = 0; j < i; j++)
if (arr[i] == arr[j])
break;
if (i == j)
System.out.print( arr[i] + " ");
}
}
public static void main (String[] args)
{
int arr[] = {6, 10, 5, 4, 9, 120, 4, 6, 10};
int n = arr.length;
printDistinct(arr, n);
}
}

PATTERN :
public class series{
public static void main (String args[]){
int i,j,row=6;
for(i=0;i<row;i++){

for(j=0;j<=i;j++){
System.out.print("* ");
}
System.out.println();
}}}

FIND CHAR IN STRING:

public class Main{


public static void main(String args[]){
String str = "Hello wold";
char target = 'o';
int index = str.indexOf(target);
if(index !=-1){
System.out.println("char " + target + " found at " +index);
}
else{
System.out.println(target);
}

}
}
inheritance with all access specifiers with static variable with constructor:
// Parent class
class Parent {
// Private variable - not accessible outside this class
private int privateVar = 10;

// Default (package-private) variable - accessible within the same package


int defaultVar = 20;

// Protected variable - accessible within the same package and subclasses


protected int protectedVar = 30;

// Public variable - accessible from anywhere


public int publicVar = 40;

// Static variable - shared among all instances of this class


static int staticVar = 50;

// Constructor
public Parent() {
System.out.println("Parent constructor called.");
}

// Getter for privateVar


public int getPrivateVar() {
return privateVar;
}

// Setter for privateVar


public void setPrivateVar(int privateVar) {
this.privateVar = privateVar;
}

// Method to display variable values


public void displayVariables() {
System.out.println("Parent Variables:");
System.out.println("privateVar: " + privateVar);
System.out.println("defaultVar: " + defaultVar);
System.out.println("protectedVar: " + protectedVar);
System.out.println("publicVar: " + publicVar);
System.out.println("staticVar: " + staticVar);
}
}
// Child class
class Child extends Parent {
// Constructor
public Child() {
System.out.println("Child constructor called.");
}

// Method to display variable values from the child class


@Override
public void displayVariables() {
// We can access protected, public, and default variables directly
System.out.println("Child Variables:");
// System.out.println("privateVar: " + privateVar); // Cannot access privateVar directly
System.out.println("privateVar (via getter): " + getPrivateVar());
System.out.println("defaultVar: " + defaultVar);
System.out.println("protectedVar: " + protectedVar);
System.out.println("publicVar: " + publicVar);
System.out.println("staticVar: " + staticVar);
}
}

// Main class to test the functionality


public class InheritanceExample {
public static void main(String[] args) {
// Create an instance of the Parent class
Parent parent = new Parent();
parent.displayVariables();

// Create an instance of the Child class


Child child = new Child();
child.displayVariables();

// Access and modify the static variable


System.out.println("\nModifying staticVar...");
Child.staticVar = 100;

// Display the static variable from both instances


System.out.println("Parent staticVar: " + Parent.staticVar);
System.out.println("Child staticVar: " + Child.staticVar);
}
}

You might also like