Posts

Java program to count words starting with capital letters in given line

Simple Java program to count words starting with capital letters in given line Code: import java.util.Scanner; class p83{public static void main(String args[]){ Scanner sc=new Scanner(System.in); String s; System.out.println("Enter a line:"); s=sc.nextLine(); int count=0; for(String str: s.split(" ")) { if(str.charAt(0)>=65 && str.charAt(0)<=90) { count++; } } System.out.println("total number of words start with capital letters are :"+count); }}

Java program to calculate occurrence of consonants and vowels in given line

Simple Java program to calculate the number of consonants and vowels present in given line Code: import java.util.Scanner; class p82{public static void main(String args[]){ String s = new String(); int co=0,cs=0; Scanner sc = new Scanner(System.in); s=sc.nextLine(); for(int i=0;i<s.length();i++){ char c = s.charAt(i); if(c=='a'||c=='e'||c=='i'||c=='o'||c=='u'||c=='A'||c=='E'||c=='I'||c=='O'||c=='U'){co++;} else if(!(c=='a'||c=='e'||c=='i'||c=='o'||c=='u'||c=='A'||c=='E'||c=='I'||c=='O'||c=='U') && ((c>=65 && c<=90) || (c>=97 && c<=122))){cs++;} } System.out.println("Vowels: "+co); System.out.println("Consonant: "+cs); }}

Java program to implement concept of Abstract Class

Simple Java program to implement concept of Abstract Class Code: abstract class phone{ void rearcamera(){System.out.println("Capturing from rear camera");} void rflesh(){System.out.println("Capturing from rear camera");} void frontcamera(){System.out.println("Capturing from front camera");} abstract void fflesh(); } class model extends phone { void fflesh(){System.out.println("No front flesh available");} } class abstracte{ public static void main(String arg[]){ model e70 = new model(); e70.rearcamera();e70.rflesh();e70.frontcamera();e70.fflesh(); } }

Java program to implement use of Final Keyword on Class, Method and Variable

Image
Simple Java program to use Final Keyword on following items: Variable Class Method Code: class a{ final int a=5; void setfinala(int x) { a=x; } final void finalmethod() { System.out.println("Final method."); } } class a1 extends a{ void finalmethod() { System.out.println("Final method in extended class."); } } final class b{ } class c extends b{ } class p33{ public static void main(String args[]){ a obj = new a(); a.setfinala(3); } } Note: The program won't run because of error and that error is output of our program.

Java program to implement concept of Inner Class

Simple Java program to implement use of Inner Class Code: class Outer{ private int data=30; class Inner{ void msg(){System.out.println("data is "+data);} } } class innersample{ public static void main(String args[]){ Outer obj=new Outer(); Outer.Inner in=obj.new Inner(); in.msg(); } }