0% found this document useful (0 votes)
24 views

Assi3 Java Funda1

The document contains examples of Java programs that demonstrate different concepts related to constructors including: default constructors, parameterized constructors, constructor overloading, calling constructors from other constructors, passing parameters to constructors, and initializing fields in constructors.
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views

Assi3 Java Funda1

The document contains examples of Java programs that demonstrate different concepts related to constructors including: default constructors, parameterized constructors, constructor overloading, calling constructors from other constructors, passing parameters to constructors, and initializing fields in constructors.
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 7

1)import java.util.

Scanner;

public class ReverseNumber {

public static void main(String[] args) {


Scanner scanner = new Scanner(System.in);

System.out.print("Enter the number: ");


int number = scanner.nextInt();

int reversedNumber = 0;
int remainder;

// Iterate through the digits of the original number


while (number != 0) {
remainder = number % 10; // Extract the last digit
reversedNumber = reversedNumber * 10 + remainder; // Shift existing
digits and add the new digit
number /= 10; // Remove the last digit from the original number
}

System.out.println("The reversed number is: " + reversedNumber);


}
}

2)import java.util.Scanner;

public class FactorialExample {

public static void main(String[] args) {


Scanner scanner = new Scanner(System.in);

System.out.print("Enter the number: ");


int number = scanner.nextInt();

if (number < 0) {
System.out.println("Invalid input: Factorial is not defined for
negative numbers.");
return;
}

// Handle 0 and 1 cases efficiently


if (number == 0) {
System.out.println("Factorial of 0 is 1.");
return;
} else if (number == 1) {
System.out.println("Factorial of 1 is 1.");
return;
}

// Choose the most efficient approach based on input size


long result;
if (number > 15) { // Use BigInteger for larger numbers to avoid overflow
result = calculateFactorialBigInteger(number);
} else { // Use int for smaller numbers for efficiency
result = calculateFactorialInt(number);
}
System.out.println("The factorial of " + number + " is: " + result);
}

private static long calculateFactorialBigInteger(int number) {


BigInteger result = BigInteger.ONE;
for (int i = 2; i <= number; i++) {
result = result.multiply(BigInteger.valueOf(i));
}
return result.longValue();
}

private static long calculateFactorialInt(int number) {


long result = 1;
for (int i = 2; i <= number; i++) {
result *= i;
}
return result;
}
}

3)import java.util.Scanner;

public class TwoArray {

public static void main(String[] args) {


Scanner scanner = new Scanner(System.in);

System.out.print("Enter the number of rows and columns (separated by


space): ");
int rows = scanner.nextInt();
int cols = scanner.nextInt();

// Use nested for loops to initialize and display the matrix


int[][] matrix = new int[rows][cols];
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
System.out.print("Enter the element at [" + i + "][" + j + "]: ");
matrix[i][j] = scanner.nextInt();
}
}

System.out.println("\nThe given matrix:");


for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
System.out.print(matrix[i][j] + " ");
}
System.out.println(); // Move to the next line after each row
}
}
}
4)import java.util.Scanner;

public class CountArray {

public static void main(String[] args) {


Scanner scanner = new Scanner(System.in);

System.out.print("Enter the array size: ");


int size = scanner.nextInt();

// Ensure valid input (size > 0)


while (size <= 0) {
System.out.println("Invalid input: Array size must be positive.");
System.out.print("Enter the array size: ");
size = scanner.nextInt();
}

int[] array = new int[size];

System.out.println("Enter the array elements:");


for (int i = 0; i < size; i++) {
array[i] = scanner.nextInt();
}

int positiveCount = 0;
int negativeCount = 0;

// Count positive and negative numbers efficiently


for (int number : array) {
if (number > 0) {
positiveCount++;
} else if (number < 0) {
negativeCount++;
}
}

// Formatted output for clarity


System.out.println("Positive numbers: " + positiveCount);
System.out.println("Negative numbers: " + negativeCount);
}
}

5)import java.util.Scanner;

public class VowelCount {

public static void main(String[] args) {


Scanner scanner = new Scanner(System.in);

System.out.print("Enter a character: ");


char ch = scanner.nextLine().charAt(0); // Get the first character entered
boolean isVowel = isVowel(ch);

if (isVowel) {
System.out.println(ch + " is a vowel.");
} else {
System.out.println(ch + " is not a vowel.");
}
}

private static boolean isVowel(char ch) {


char lower = Character.toLowerCase(ch); // Convert to lowercase for case-
insensitivity
return lower == 'a' || lower == 'e' || lower == 'i' || lower == 'o' ||
lower == 'u';
}
}

6)public class AccountTest {

public static void main(String[] args) {


// Create an account with only account number (default constructor)
Account account1 = new Account(101);
System.out.println("Account with only account number:");
account1.display();

// Create an account with account number and customer name


Account account2 = new Account(102, "Vishal");
System.out.println("\nAccount with account number and customer name:");
account2.display();
}
}

class Account {

private int accountNumber;


private String customerName;

// Default constructor (for account number only)


public Account(int accountNumber) {
this.accountNumber = accountNumber;
}

// Constructor with account number and customer name


public Account(int accountNumber, String customerName) {
this.accountNumber = accountNumber;
this.customerName = customerName;
}

// Display account details


public void display() {
System.out.println("Account number: " + accountNumber);
if (customerName != null) {
System.out.println("Customer name: " + customerName);
}
}
}

7)public class DefaultAccount {

private int accountNumber;


private String name;
private String address;

// Default constructor with pre-assigned values


public DefaultAccount() {
this.accountNumber = 101111;
this.name = "Vimal";
this.address = "Panchkula";
}

// Display account details


public void display() {
System.out.println("Account number: " + accountNumber);
System.out.println("Name: " + name);
System.out.println("Address: " + address);
}

public static void main(String[] args) {


// Create an instance of DefaultAccount
DefaultAccount account = new DefaultAccount();

// Display the account details


account.display();
}
}

8)public class ConsCallmeth {

private int a;
private int b;
private int sum;

public ConsCallmeth(int a, int b) {


this.a = a;
this.b = b;
this.sum = add(); // Call the add() method within the constructor
}

public int add() {


return a + b;
}

public void displaySum() {


System.out.println("The sum of " + a + " and " + b + " is: " + sum);
}

public static void main(String[] args) {


ConsCallmeth obj = new ConsCallmeth(5, 6); // Create an instance with pre-
defined values
obj.displaySum(); // Display the calculated sum
}
}

9)public class ConsParamPass {

private int num1;


private int num2;

// Constructor to initialize with two numbers


public ConsParamPass(int num1, int num2) {
this.num1 = num1;
this.num2 = num2;
}

// Calculate and display the sum


public void displaySum() {
int sum = num1 + num2;
System.out.println("The sum of " + num1 + " and " + num2 + " is: " + sum);
}

// Calculate and display the difference


public void displayDifference() {
int difference = num1 - num2;
System.out.println("The difference of " + num1 + " and " + num2 + " is: " +
difference);
}

public static void main(String[] args) {


// Create an instance with pre-defined values
ConsParamPass obj = new ConsParamPass(4, 5);

// Display the calculated sum and difference


obj.displaySum();
obj.displayDifference();
}
}

10)public class ConstThisOver {

private int employeeNumber;

// Default constructor calling the parameterized constructor


public ConstThisOver() {
this(10011); // Call the parameterized constructor with a default value
}

// Parameterized constructor to initialize employeeNumber


public ConstThisOver(int employeeNumber) {
this.employeeNumber = employeeNumber;
System.out.println("Employee number: " + employeeNumber);
}
public static void main(String[] args) {
// Create an instance using the default constructor (which calls the
parameterized constructor)
ConstThisOver obj = new ConstThisOver();
}
}

11)public class EmpConsOver {

private String name;


private int number;

// Constructor to initialize name and number


public EmpConsOver(String name, int number) {
this.name = name;
this.number = number;
}

// Display employee details


public void display() {
System.out.println("Employee name: " + name);
System.out.println("Employee no: " + number);
}

public static void main(String[] args) {


// Create objects and call display() method
EmpConsOver emp1 = new EmpConsOver("Akil", 101);
emp1.display();

EmpConsOver emp2 = new EmpConsOver("Arun", 102);


emp2.display();

EmpConsOver emp3 = new EmpConsOver("Rajesh", 103);


emp3.display();

EmpConsOver emp4 = new EmpConsOver("Vishal", 104);


emp4.display();
}
}

You might also like