0% found this document useful (0 votes)
14 views

Programs

Uploaded by

Jathin
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)
14 views

Programs

Uploaded by

Jathin
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/ 15

Strings:

Output:

modem is an electronic device

modem

device

number of consequtive pairs2

import java.util.Scanner;

public class modem

public static void main(String args[])

Scanner in=new Scanner(System.in);

int i,f=0,c=0;

char ch1,ch2;

String word="";

String str=in.nextLine();

str=str+" ";

//String search=in.nextLine();

//s=s.toLowerCase();

//int f=0;

for(i=0;i<str.length();i++)

char ch=str.charAt(i);

if(ch==' ')

for(int j=0;j<word.length()-1;j++)

ch2=word.charAt(j);

ch1=word.charAt(j+1);

if(ch1==(char)(ch2+1))

{
//f=1;

System.out.println(word);

c++;

break;

word="";

else

word=word+ch;

System.out.println("number of consequtive pairs"+c);

Letter frequency in the sentence

import java.util.Scanner;

public class frequency

public static void main(String args[])

Scanner in=new Scanner(System.in);

int i;

String test="";

String s=in.nextLine();

s=s.toUpperCase();
int freq[]=new int[26];

for(i=0;i<s.length();i++)

char ch=s.charAt(i);

if(Character.isLetter(ch))

int index=(int)ch-65;

freq[index]++;

for(int j=0;j<26;j++)

if(freq[j]!=0)

System.out.println((char)(j+65)+"\t"+freq[j]);

computer applications

A 2

C 2

E 1

I 2

L 1

M 1

N 1

O 2

P 3

R 1
S 1

T 2

U 1

Word frequency:

Enter a sentence: The lazy fox jumped over the wall

Enter the word: the

import java.util.Scanner;

public class wordfreq {

public static void main(String[] args)

String s1="";

Scanner sc = new Scanner(System.in);

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

String s = sc.nextLine().toUpperCase()+" ";

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

String w = sc.next().toUpperCase();

int j = 0;

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

char ch = s.charAt(i);

if(ch!=' ')

s1 = s1 + ch;

if(ch==' ')

if (s1.equals(w))

j++;

}
s1 ="";

System.out.print(j);

User defined methods:

import java.util.*;

public class twinprime

public boolean prime(int n)

int f=0;

for (int i= 2;i<=n/2;i++)

if (n%i==0)

f=1;

break;

if (f==0) {

return true;

else

return false;

public static void main(String args[])

{
twinprime ob=new twinprime();

Scanner sc=new Scanner(System.in);

int a1=sc.nextInt();

int a2=sc.nextInt();

boolean r1=ob.prime(a1);

boolean r2=ob.prime(a2);

if(r1==true && r2==true && Math.abs(a1-a2)==2)

System.out.println(a1+" "+a2+" are twin primes");

else

System.out.println(a1+" "+a2+" are not twin primes");

Method overloading:

import java.util.Scanner;

public class Methodoverload

public void display()

for(int i = 1; i <= 5; i++)

for(int j = 1; j <= i; j++)

System.out.print(j + " ");

System.out.println();

public void display(int n)

{
while( n != 0)

int d = n % 10;

System.out.println(Math.sqrt(d));

n = n / 10;

public static void main(String args[])

Methodoverload obj = new Methodoverload();

Scanner in = new Scanner(System.in);

System.out.println("Pattern: ");

obj.display();

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

int num = in.nextInt();

obj.display(num);

Class based:

import java.util.Scanner;

public class Mobike

int bno;

int phno;

int days;

int charge;

String name;

public void input() {


Scanner in = new Scanner(System.in);

System.out.print("Enter Customer Name: ");

name = in.nextLine();

System.out.print("Enter Customer Phone Number: ");

phno = in.nextInt();

System.out.print("Enter Bike Number: ");

bno = in.nextInt();

System.out.print("Enter Number of Days: ");

days = in.nextInt();

public void compute() {

if (days <= 5)

charge = days * 500;

else if (days <= 10)

charge = (5 * 500) + ((days - 5) * 400);

else

charge = (5 * 500) + (5 * 400) + ((days - 10) * 200);

public void display() {

System.out.println("Bike No.\tPhone No.\tName\tNo. of days \tCharge");

System.out.println(bno + "\t" + phno + "\t" + name + "\t" + days

+ "\t" + charge);

public static void main(String args[]) {

Mobike obj = new Mobike();

obj.input();

obj.compute();

obj.display();

}
Default Constructor

import java.util.Scanner;

public class FruitJuice

int product_code;

String flavour;

String pack_type;

int pack_size;

int product_price;

public FruitJuice() {

product_code = 0;

flavour = "";

pack_type = "";

pack_size = 0;

product_price = 0;

public void input() {

Scanner in = new Scanner(System.in);

System.out.print("Enter Flavour: ");

flavour = in.nextLine();

System.out.print("Enter Pack Type: ");

pack_type = in.nextLine();

System.out.print("Enter Product Code: ");

product_code = in.nextInt();

System.out.print("Enter Pack Size: ");

pack_size = in.nextInt();

System.out.print("Enter Product Price: ");


product_price = in.nextInt();

public void discount() {

product_price -= 10;

public void display() {

System.out.println("Product Code: " + product_code);

System.out.println("Flavour: " + flavour);

System.out.println("Pack Type: " + pack_type);

System.out.println("Pack Size: " + pack_size);

System.out.println("Product Price: " + product_price);

public static void main(String args[]) {

FruitJuice obj = new FruitJuice();

obj.input();

obj.discount();

obj.display();

Parameterised Constructor

import java.util.Scanner;

public class Employee

String ename;

int ecode;

double basicpay;
public Employee(String name,int code,double sal)

ename = name;

ecode = code;

basicpay = sal;

public double salCalc()

double hra = 20.0/100.0 * basicpay;

double da = 25.0/100.0 * basicpay;

double ta = 10.0/100.0 * basicpay;

double allowance;

double sal = basicpay + hra + da + ta;

if (ecode <= 100) {

allowance = 20.0/100.0 * sal;

if(allowance > 2500)

allowance = 2500;

else {

allowance = 1000;

double tsal = sal + allowance;

return tsal;

public void display() {

double totalsal = salCalc();

System.out.println("Name : " + ename);

System.out.println("Employee Code : " + ecode);

System.out.println("Basic Pay : Rs. " + basicpay);


System.out.println("Salary : " + totalsal);

public static void main() {

Scanner in = new Scanner(System.in);

double totalsal;

System.out.print("Enter Name : ");

String name = in.nextLine();

System.out.print("Enter Employee Code : ");

int empcode = in.nextInt();

System.out.print("Enter Basic Pay : ");

double basicsal = in.nextDouble();

Employee obj = new Employee(name,empcode,basicsal);

obj.salCalc();

obj.display();

Static Variables:

import java.util.Scanner;

public class staticvars

String ename;

int eno;

int count;

double basicpay;

public void accept(String ename1,int eno1)

ename=ename1;

eno=eno1;

}
public void calculate()

count++;

public void display()

System.out.println("ename is:"+ename);

System.out.println("eno is:"+eno);

System.out.println("count is:"+count);

public static void main(String args[])

//System.out.println(count);

staticvars ob1=new staticvars();

staticvars ob2=new staticvars();

staticvars ob3=new staticvars();

ob1.accept("jai",45);

ob2.accept("charan",70);

ob3.accept("jia",100);

ob1.calculate();

ob2.calculate();

ob3.calculate();

ob3.display();

ob1.display();

ob2.display();

}
}

This Keyword:

import java.util.Scanner;

public class thiskeyword

String ename;

int eno;

static int count;

double basicpay;

public void accept(String ename,int eno)

this.ename=ename;

this.eno=eno;

public void calculate()

count++;

public void display()

System.out.println("ename is:"+ename);

System.out.println("eno is:"+eno);

System.out.println("count is:"+count);

public static void main(String args[])

{
System.out.println(count);

thiskeyword ob1=new thiskeyword();

thiskeyword ob2=new thiskeyword();

thiskeyword ob3=new thiskeyword();

ob1.accept("jai",45);

ob2.accept("charan",70);

ob3.accept("jia",100);

ob1.calculate();

ob2.calculate();

ob3.calculate();

ob3.display();

ob1.display();

ob2.display();

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy