Thursday, 27 September 2018

Develop a program that creates a vector to insert and display five elements of different data types

Develop a program that creates a vector to insert and display five elements of different data types

Program:


import java.util.*;
class VectorDemo
{
                public static void main(String args[])
                {
                                Vector v = new Vector();
                                v.addElement(new Integer(10));
                                v.addElement(new Float(1.2));
                                v.addElement(new Double(5.2));
                                v.addElement(new Character('A'));
                                v.addElement(new String("Ashish"));
                                int s = v.size();
                                for(int i=0;i<s;i++)
                                System.out.println(v.elementAt(i));
                }
}

Output:


Develop a program to convert a string from lowercase to uppercase using method of String class.

Develop a program to convert a string from lowercase to uppercase using method of String class.

Program:

import java.util.*; 
class ConvertCasePrg
{
   public static void main(String args[])
   {
Scanner sc= new Scanner(System.in);
String str="";

//input string
System.out.print("Enter any string: ");
str=sc.nextLine();

//declaring objects to store lowercase and uppercase strings
String lowerCaseString="",upperCaseString="";

//convert into lower case
lowerCaseString= str.toLowerCase();  
//convert into upper case
upperCaseString= str.toUpperCase();  

//printing the strings
System.out.println("Original String: "+str);
System.out.println("Lower Case String: "+lowerCaseString);
System.out.println("Upper Case String: "+upperCaseString);   
   }

}

Output:



Develop a program to create a class Student with data membersstudent_name, roll_no& branch. Initialize and display values of data members.

Develop a program to create a class Student with data members student_name, roll_no&  branch. Initialize and display values of data members.

Program:

import java.lang.*;
import java.io.*;
class Student
{
 String name;
 int roll_no;
 String branch;

 void getdata() throws IOException {
 
  BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  System.out.println ("Enter Name of Student");
  name = br.readLine();
 
  System.out.println ("Enter Roll No. of Student");
  roll_no = Integer.parseInt(br.readLine());
 
  System.out.println ("Enter branch name");
  branch = br.readLine();

  }

 void show() {
 
  System.out.println ("Roll No. = "+roll_no);
  System.out.println ("Name = "+name);
  System.out.println ("Enter branch name = "+branch);
   }
}

public class StudentDemo {

 public static void main(String[] args) throws IOException {
 
  Student s=new Student();
  s.getdata();
  s.show();
 }
}

Output:



Number Pattern Printing Programming

How to print number pattern programming

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

Output:



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

Output:



class NumberPattern
{
public static void main(String[] args)
{
for (int i = 1; i <= 6; i++)
  {
    for (int j = 1; j <= i; j++)
     {
       System.out.print(j+" ");
     }
for (int j = i-1; j >= 1; j--)
  {
      System.out.print(j+" ");
    }
    System.out.println();
   }
}
}

Output: