0% found this document useful (0 votes)
4 views4 pages

JAVA LAB ASSIGNMENT 6

The document contains several Java programming exercises, including methods for displaying the middle character of a string, counting vowels, computing the sum of digits, and demonstrating method overloading and constructors. Each exercise includes code snippets and explanations for implementation. The exercises cover fundamental Java concepts and provide practical coding examples.

Uploaded by

darkemperor687
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)
4 views4 pages

JAVA LAB ASSIGNMENT 6

The document contains several Java programming exercises, including methods for displaying the middle character of a string, counting vowels, computing the sum of digits, and demonstrating method overloading and constructors. Each exercise includes code snippets and explanations for implementation. The exercises cover fundamental Java concepts and provide practical coding examples.

Uploaded by

darkemperor687
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/ 4

JAVA LAB ASSIGNMENT 6

1. Write a Java method to display the middle character of a string.


import java.util.Scanner;
public class Exercise3 {
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
System.out.print("Input a string: ");
String str = in.nextLine();
System.out.print("The middle character in the string: " + middle(str)+"\n");
}
public static String middle(String str)
{
int position;
int length;
if (str.length() % 2 == 0)
{
position = str.length() / 2 - 1;
length = 2;
}
else
{
position = str.length() / 2;
length = 1;
}
return str.substring(position, position + length);
}
}

2. Write a Java method to count all vowels in a string.

import java.util.Scanner;
public class Exercise4 {
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
System.out.print("Input the string: ");
String str = in.nextLine();

System.out.print("Number of Vowels in the string: " + count_Vowels(str)+"\n");


}
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;
}
}
3. Write a Java method to compute the sum of digits in an integer.
import java.util.Scanner;
public class Exercise6 {

public static void main(String[] args)


{
Scanner in = new Scanner(System.in);
System.out.print("Input an integer: ");
int digits = in.nextInt();
System.out.println("The sum is " + sumDigits(digits));
}

public static int sumDigits(long n) {


int result = 0;

while(n > 0) {
result += n % 10;
n /= 10;
}

return result;
}

4. Write a java program to add numbers by using method overloading

public class Sum {


public int sum(int x, int y)
{
return (x + y);

public int sum(int x, int y, int z)


{
return (x + y + z);
}

public double sum(double x, double y)


{
return (x + y);
}

public static void main(String args[])


{
Sum s = new Sum();
System.out.println(s.sum(10, 20));
System.out.println(s.sum(10, 20, 30));
System.out.println(s.sum(10.5, 20.5));
}
}

5. Write a Java program to demonstrate a no-argument constructor


class MyClass
{
MyClass()
{
System.out.println("No-argument constructor called");
}

public static void main(String[] args)


{
MyClass obj = new MyClass();
}
}

6. Create a program that shows constructor overloading in Java

class Box
{
double width, height, depth;

Box()
{
width = 1;
height = 1;
depth = 1;
}

Box(double w, double h, double d)


{
width = w;
height = h;
depth = d;
}

void myMethod()
{
System.out.println("Width : " + width);
System.out.println("Height : " + height);
System.out.println("Depth : " + depth);
}

public static void main(String[] args)


{
Box box1 = new Box();
Box box2 = new Box(3, 8, 6);
box1.myMethod();
box2.myMethod();

}
}

7. Implement a parameterized constructor to initialize an object

class Person
{
String name;

Person(String n)
{
name = n;
}

public static void main(String[] args)


{
Person person = new Person("Sam Kumar");
System.out.println("Name : " + person.name);
}
}

You might also like