Professional Documents
Culture Documents
Course : Msc.It
Subject : Java Programming
Roll No. : 11
Faculty Name : Priti Patel
Assignment 9
1. Write a program to count a number of vowels in a String
using userdefined count().
Code:
package com.mycompany.ass8_java;
import java.util.Scanner;
public class vowels {
public static int count_Vowels(String str)
{
int count = 0;
for (int i = 0; i < str.length(); i++)
{
if (str.charAt(i) == 'a' || str.charAt(i) == 'e' || str.charAt(i) == 'i'
|| str.charAt(i) == 'o' || str.charAt(i) == 'u')
{
count++;
}
}
return count;
}
public static void main(String[] args)
{
System.out.println("Input the string: ");
Scanner sc = new Scanner(System.in);
String str = sc.nextLine();
System.out.println("Number of Vowels in the string: " +
count_Vowels(str)+"\n");
}
}
Output:
import java.util.Arrays;
import java.util.Arrays;
8. Write a Java program to move all 0's to the end of an array using
move() method.Maintain the relative order of the other (non-
zero) array elements.
Code:
package com.mycompany.ass8_java;
public class move_array {
public static void main(String[] args)
{
int arr[] = {1, 9, 8, 4, 0, 0, 2, 7, 0, 6, 0, 9};
int n = arr.length;
move(arr, n);
System.out.println("Array after pushing zeros to the back: ");
for (int i=0; i<n; i++)
{
System.out.print(arr[i]+" ");
}
}
static void move(int arr[], int n)
{
int count = 0;
for (int i = 0; i < n; i++)
{
if (arr[i] != 0)
{
arr[count++] = arr[i];
}
}
while (count < n)
{
arr[count++] = 0;
}
}
}
Output:
Output:
Code:
package com.mycompany.ass8_java;
import java.util.Scanner;
public class password {
{
Scanner input = new Scanner(System.in);
System.out.print(
"1. A password must have at least eight characters.\n" +
String s = input.nextLine();
if (is_Valid_Password(s))
{
System.out.println("Password is valid: " + s);
}
else
{
System.out.println("Not a valid password: " + s);
}
}
return false;
}
int charCount = 0;
int numCount = 0;
char ch = password.charAt(i);
if (is_Numeric(ch))
{
numCount++;
}
else if (is_Letter(ch))
{
charCount++;
}
else
{
return false;
}
}
{
ch = Character.toUpperCase(ch);
Output:
vehicle(String color)
{
this.color=color;
}
}
public class bus extends vehicle{
bus(String color)
{
super(color);
}
void display()
{
System.out.println("Bus Color: "+ color);
}
public static void main(String[] args)
{
bus b=new bus("black");
b.display();
}
}
Output:
class person
{
int p_id;
String p_name;
this.p_id=p_id;
this.p_name=p_name;
}
}
super(p_id,p_name);
this.p_salary=p_salary;
}
void display()
{
System.out.println("Employee ID: "+p_id);
}
public static void main(String[] args)
{
employee e=new employee(101,"Aastha Oza",25500);
e.display();
}
}
Output:
package com.mycompany.ass8_java;
public class sbi extends bank{
@Override
public int getRateOfInterest()
{
return 7;
}
}
package com.mycompany.ass8_java;
public class bob extends bank{
@Override
public int getRateOfInterest()
{
return 6;
}
}
package com.mycompany.ass8_java;
public class icici extends bank{
@Override
public int getRateOfInterest()
{
return 3;
}
}
package com.mycompany.ass8_java;
public class testbank {
public static void main(String[] args)
{
bank b=new bob();
bank i=new icici();
bank s=new sbi();
System.out.println("Rate of Interest in BOB is:
"+b.getRateOfInterest()+" %");
System.out.println("Rate of Interest in ICICI is:
"+i.getRateOfInterest()+" %");
System.out.println("Rate of Interest in SBI is: "+s.getRateOfInterest()
+" %");
}
}
Output: