Core Java Program Set 1

Program 1: First.java

public class First {

        public static void main(String[] args) {
              
                System.out.println("Welcome To Java!!!");
        }
}


Program 2: Pyramid1.java
/*
        Java Pyramid 1 Example
        This Java Pyramid example shows how to generate pyramid or triangle like
        given below using for loop.
      
        *
        **
        ***
        ****
        *****
*/

public class Pyramid1 {

        public static void main(String[] args) {
              
                for(int i=1; i<= 5 ;i++){
                      
                        for(int j=0; j < i; j++){
                                System.out.print("*");
                        }
                      
                        //generate a new line
                        System.out.println("");
                }
        }
}

Program 3: Pyramid2.java
/*
        Java Pyramid 3 Example
        This Java Pyramid example shows how to generate pyramid or triangle like
        given below using for loop.
      
        1
        12
        123
        1234
        12345

*/
public class JavaPyramid4 {

        public static void main(String[] args) {
              
                for(int i=1; i<= 5 ;i++){
                      
                        for(int j=0; j < i; j++){
                                System.out.print(j+1);
                        }
                      
                        System.out.println("");
                }

        }
}

Program 4: Factorial.java
//Factorial Program using loop in java

class Factorial{  
public static void main(String args[])
{
int i, fact=1;  
int number=5;//It is the number to calculate factorial    
for(i=1; i<=number; i++){
fact=fact*i;
}    
System.out.println("Factorial of "+number+" is: "+fact);
}
}

Program 5: StringExample.java
//String Example in Java

public class StringExample
{       
public static void main(String[] args)
          {       
String s1 = "Computer Science";
                   int x = 307;
                   String s2 = s1 + " " + x;
                   String s3 = s2.substring(10,17);
                   String s4 = "is fun";
                   String s5 = s2 + s4;
                  
                   System.out.println("s1: " + s1);
                   System.out.println("s2: " + s2);
                   System.out.println("s3: " + s3);
                   System.out.println("s4: " + s4);
                   System.out.println("s5: " + s5);
                  
                   //showing effect of precedence
                  
                   x = 3;
                   int y = 5;
                   String s6 = x + y + "total";
                   String s7 = "total " + x + y;
                   String s8 = " " + x + y + "total";
                   System.out.println("s6: " + s6);
                   System.out.println("s7: " + s7);
                   System.out.println("s8: " + s8);
          }
}

Program 6: AreaOfCircle.java
//WAP to calculate Area of Circle

import java.util.Scanner;
class AreaOfCircle
{
   public static void main(String args[])
    {  
      
          Scanner s= new Scanner(System.in);
        
         System.out.println("Enter the radius:");
         double r= s.nextDouble();
         double  area=(22*r*r)/7 ;
         //double  area=3.14*r*r ;

         System.out.println("Area of Circle is: " + area);      
   }
}

Program 7: ElectricityBill.java
//Electricity Bill Program using class

import java.util.*;
class ElectricityBill
{       
double billpay;

CalculateElectricityBill(long units)
{
if(units<100)
          billpay=units*1.20;
else if(units<=300)
          billpay=100*1.20+(units-100)*2;
else if(units>300)
          billpay=100*1.20+ 200*2 + (units-300)*3;
 }      
}

class ComputeElectricityBill
{
public static void main(String args[])
{  
          long units;
          Scanner sc=new Scanner(System.in);
          System.out.println("enter number of units");
          units=sc.nextLong();
          CalculateElectricityBill b=new CalculateElectricityBill(units);
          System.out.println("Bill to pay : " + b.billpay);
}
}


Program 8: Vowel.java
//Java Program to Print Whether the Given Alphabet is Vowel or Consonant

import java.util.Scanner;
class Vowel
{
          public static void main(String[ ] arg)
          {
          int i=0;
          Scanner sc=new Scanner(System.in);
          System.out.println("Enter a character : ");
          char ch=sc.next( ).charAt(0);  
          //char ch=sc.nextChar();
          switch(ch)
          {
           case  'a' :
           case 'e'  :
           case 'i'   :
           case 'o'  :
           case 'u'  :
           case  'A' :
           case 'E'  :
           case 'I'   :
           case 'O'  :
           case 'U'  :i++;
          }
                    if(i==1)
                    System.out.println("Entered character "+ch+" is  Vowel");
                    else if((ch>='a'&&ch<='z')||(ch>='A'&&ch<='Z'))
                   System.out.println("Entered character "+ch+" is Consonent");
                   else
                   System.out.println("Not an alphabet");              
          }
}

Program 9: Sum.java
//Calculate Sum of N Numbers Using While Loop

import java.util.Scanner;
class Sum
{
          public static void main(String arg[])
          {
                   int n,sum=0,i=0;                
Scanner sc=new Scanner(System.in);
System.out.println("Enter how many numbers you want sum");
                   n=sc.nextInt();
                   int a[]=new int[n];
System.out.println("Enter the "+n+" numbers ");
                   while (i<n)
                             {      
System.out.println("Enter  number  "+(i+1)+":");
a[i]=sc.nextInt();
sum+=a[i];    
i++;    
}
System.out.println("sum is ="+sum);                  
}
}


Program 10: StudentInfo.java
//Display Student information using class

class Student
{
          int rno;
          string name;
          double per;

          public void getData()
{
          rno=1;
          name=”Piyush”;
          per=82.67;
}

public void dispData()
{
          System.out.println(“     Student Information       ”);
          System.out.println(“----------------------------------”);
          System.out.println(“Roll Number:” + rno);
          System.out.println(“Name            :” + name);
          System.out.println(“Percentage    :” + per);
}
}

class StudentInfo
{
          public static void main(String arg[])
          {
                   Student s1 = new Student();
s1.getData();
s1.dispData();
          }

}

Comments

Popular posts from this blog

પટેલ સમાજનો ઈતિહાસ જાણો : કોણ અને ક્યાંથી આવ્યા હતા પાટીદારો

Python HTML Generator using Yattag Part 1

Java Event Delegation Model, Listener and Adapter Classes