0% found this document useful (0 votes)
17 views17 pages

Java Programming Assignment Solutions

The document contains a series of Java programming assignments covering basic concepts such as displaying messages, arithmetic operations, loops, pattern printing, and user input handling. Each assignment is accompanied by a solution in Java code along with sample output. The topics also include conditional statements, arrays, and class design for a bank account representation.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views17 pages

Java Programming Assignment Solutions

The document contains a series of Java programming assignments covering basic concepts such as displaying messages, arithmetic operations, loops, pattern printing, and user input handling. Each assignment is accompanied by a solution in Java code along with sample output. The topics also include conditional statements, arrays, and class design for a bank account representation.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

JAVA ASSIGNMENT LAB-1

1. Display message on computer screen?

SOL: package LAB1ASSIGN;

public class QUES1 {

public static void main(String[] args) {

[Link]("Chinmay Kansal");

OUTPUT: Chinmay Kansal

2. Write a java Program To Print all Alphabet?


Hint : for( int i=65 ;i<91;i++)
[Link]((char)i);

SOL: package LAB1ASSIGN;

public class QUES2 {

public static void main(String[] args) {

for(int i=65;i<=90;i++) {

[Link]((char)i+" ");

OUTPUT: A B C D E F G H I J K L M N O P Q R S T U V W X Y Z

3. Write a java Program that perform all the arithmetic operation?

SOL: package LAB1ASSIGN;

import [Link].*;

public class QUES3 {


public static void main(String[] args) {

Scanner sc = new Scanner([Link]);

[Link]("Enter first number: ");

double a = [Link]();

[Link]("Enter second number: ");

double b = [Link]();

[Link]("Addition is "+ (a+b));

[Link]("Subtraction is " + (a-b));

[Link]("Multiplication is " + (a*b));

double quotient = (b != 0) ? (a / b) : [Link]; // check division by zero

double modulus = (b != 0) ? (a % b) : [Link];

[Link]("Quotient is "+quotient);

[Link]("Modulus is "+ modulus);

[Link]();

OUTPUT: Enter first number: 20

Enter second number: 30

Addition is 50.0

Subtraction is -10.0

Multiplication is 600.0

Quotient is 0.6666666666666666

Modulus is 20.0
4. Write a java Program that print the multiplication table of any number?

SOL: package LAB1ASSIGN;

import [Link].*;

public class QUES4 {

public static void main(String[] args) {

Scanner sc = new Scanner([Link]);

[Link]("Enter the number: ");

int n = [Link]();

for(int i=1;i<=10;i++) {

[Link](n + " x " + i + " = " + (n * i));

[Link]();

OUTPUT: Enter the number:

5x1=5

5 x 2 = 10

5 x 3 = 15

5 x 4 = 20

5 x 5 = 25

5 x 6 = 30

5 x 7 = 35

5 x 8 = 40

5 x 9 = 45

5 x 10 = 50
5. Write a java Program to check weather the number is odd or even?

SOL: package LAB1ASSIGN;

import [Link].*;

public class QUES5 {

public static void main(String[] args) {

Scanner sc = new Scanner([Link]);

[Link]("Enter the number: ");

int n = [Link]();

if(n%2==0) [Link]("Even");

else [Link]("Odd");

[Link]();

OUTPUT: Enter the number:

20

Even

6. Write a java Program that reverse the the given number?

SOL: package LAB1ASSIGN;

import [Link].*;

public class QUES6 {

public static void main(String[] args) {

Scanner sc = new Scanner([Link]);

[Link]("Enter the number of numbers you want to enter: ");

int n = [Link]();

int arr[] = new int[n];

[Link]("Elements are: ");


for(int i=0;i<n;i++) {

arr[i] = [Link]();

[Link]("Reversed order is: ");

for(int i=n-1;i>=0;i--) {

[Link](arr[i]);

[Link]();

OUTPUT: Enter the number of numbers you want to enter:

Elements are:

25

32

Reversed order is:

32

25
7. Write a program in java to print patterns
a) * b) *
* * **
* * * ***
* * * * ****
*****
c) 1 d) ABCDEDCBA
12 ABCD DCBA
123 ABC CBA
1234 AB BA
12345 A A

E) 1
22
333
4444
55555
666666

SOL: a) package LAB1ASSIGN;

import [Link].*;

public class QUES7a {

public static void main(String[] args) {

Scanner sc = new Scanner([Link]);

[Link]("Enter the no of rows: ");

int n = [Link]();

for(int i=1;i<=n;i++) {

for(int j=i;j<n;j++) {

[Link](" ");

for(int k=1;k<=i;k++) {

[Link]("* ");

}
[Link]();

[Link]();

OUTPUT: Enter the no of rows:

* *

* * *

* * * *

b) package LAB1ASSIGN;

import [Link].*;

public class QUES7b {

public static void main(String[] args) {

Scanner sc = new Scanner([Link]);

[Link]("Enter the number of rows: ");

int a = [Link]();

for(int i=1;i<=a;i++) {

for(int j=1;j<=i;j++) {

[Link]("*");

[Link](" ");

[Link]();

}
OUTPUT: Enter the number of rows:

**

***

****

*****

c) package LAB1ASSIGN;

import [Link].*;

public class QUES7c {

public static void main(String[] args) {

Scanner sc = new Scanner([Link]);

[Link]("Enter now of rows: ");

int n = [Link]();

int k=1;

for(int i=1;i<=n;i++) {

k=1;

for(int j=1;j<=i;j++) {

[Link](k++);

[Link]();

[Link]();

OUTPUT: Enter now of rows:

5
1

12

123

1234

12345

d) package LAB1ASSIGN;

import [Link].*;

public class QUES7d {

public static void main(String[] args) {

Scanner sc = new Scanner([Link]);

[Link]("Enter number of rows: ");

int n = [Link]();

for (int i = 0; i < n; i++) {

for (char ch = 'A'; ch <= 'E' - i; ch++) {

[Link](ch);

int spaces = 2 * i - 1;

for (int s = 0; s < spaces; s++) {

[Link](" ");

if(i==0) {

for (char ch = (char) ('D' - i); ch >= 'A'; ch--) {

[Link](ch);

if (i != 0) {

for (char ch = (char) ('E' - i); ch >= 'A'; ch--) {

[Link](ch);
}

[Link]();

[Link]();

OUTPUT: Enter number of rows:

ABCDEDCBA

ABCD DCBA

ABC CBA

AB BA

A A

e) package LAB1ASSIGN;

import [Link].*;

public class QUES7e {

public static void main(String[] args) {

Scanner sc = new Scanner([Link]);

[Link]("Enter now of rows: ");

int n = [Link]();

for(int i=1;i<=n;i++) {

for(int j=1;j<=i;j++) {

[Link](i);

[Link]();

[Link]();
}

OUTPUT: Enter now of rows:

22

333

4444

55555

666666

8. Write a program to ask department name from user . If department name is accounts
add bonus of 2000 to salary and incentive zero and if department is marketing then add
incentive of 3000 and bonus zero . Check department name using switch case and
display complete record .
(use string literal in switch case)

SOL: package LAB1ASSIGN;

import [Link].*;

public class QUES8 {

public static void main(String[] args) {

Scanner sc = new Scanner([Link]);

[Link]("Enter employee name: ");

String name = [Link]();

[Link]("Enter department (accounts/marketing): ");

String department = [Link]().toLowerCase();

[Link]("Enter basic salary: ");

double salary = [Link]();

double bonus = 0;

double incentive = 0;
switch (department) {

case "accounts":

bonus = 2000;

incentive = 0;

break;

case "marketing":

bonus = 0;

incentive = 3000;

break;

default:

[Link]("Invalid department. No bonus or incentive applied.");

break;

double totalSalary = salary + bonus + incentive;

[Link]("Name : " + name);

[Link]("Department : " + department);

[Link]("Basic Salary : " + salary);

[Link]("Bonus : " + bonus);

[Link]("Incentive : " + incentive);

[Link]("Total Salary : " + totalSalary);

[Link]();

OUTPUT: Enter employee name: ABCD

Enter department (accounts/marketing): accounts

Enter basic salary: 50000

Name : ABCD

Department : accounts
Basic Salary : 50000.0

Bonus : 2000.0

Incentive : 0.0

Total Salary : 52000.0

9. Write a program to find sum of all integers greater than 100 and less than 200 that are
divisible by 7.

SOL: package LAB1ASSIGN;

public class QUES9 {

public static void main(String[] args) {

int sum=0;

for(int i=100;i<200;i++) {

if(i%7==0) {

sum=sum+i;

[Link](sum);

OUTPUT: 2107

10. Write a program to ask name ,age and salary of a employee and check if his salary is
less than 30000 print salary after adding 20% along with other details.

SOL: package LAB1ASSIGN;

import [Link].*;

public class QUES10 {

public static void main(String[] args) {

Scanner sc = new Scanner([Link]);


[Link]("Enter Name: ");

String name = [Link]();

[Link]("Enter Age: ");

int age = [Link]();

[Link]("Enter Salary: ");

double sal = [Link]();

if(sal<30000) {

[Link]("Name is: " + name);

[Link]("Age is: "+age);

[Link]("Salary is: " + (sal * 1.2));

}else {

[Link]("Name is: " + name);

[Link]("Age is: "+age);

[Link]("Salary is: " + sal);

[Link]();

OUTPUT: Enter Name:

ABCD

Enter Age:

20

Enter Salary:

50000

Name is: ABCD

Age is: 20

Salary is: 50000.0


11. Design a class to represent bank account. Include members:
a. Data members
a.i. Name of depositor
[Link]. Account number
[Link]. Type of account
[Link]. Balance amount
b. Methods
b.i. To assign value
[Link]. To deposit amount
[Link]. To withdraw amount
[Link]. To display name and balance

SOL: package LAB1ASSIGN;

import [Link].*;

public class QUES11 {

private String depositorName;

private String accountNumber;

private String accountType;

private double balance;

public void assignValues(String name, String accNumber, String accType, double


initialBalance) {

depositorName = name;

accountNumber = accNumber;

accountType = accType;

balance = initialBalance;

public void deposit(double amount) {

if (amount > 0) {

balance += amount;

[Link]("Deposited: " + amount);

} else {

[Link]("Invalid deposit amount.");


}

public void withdraw(double amount) {

if (amount > 0 && amount <= balance) {

balance -= amount;

[Link]("Withdrawn: " + amount);

} else {

[Link]("Insufficient balance or invalid amount.");

public void display() {

[Link]("Depositor Name: " + depositorName);

[Link]("Current Balance: $" + balance);

public static void main(String[] args) {

Scanner sc = new Scanner([Link]);

QUES11 account = new QUES11();

[Link]("Enter depositor name: ");

String name = [Link]();

[Link]("Enter account number: ");

String accNumber = [Link]();

[Link]("Enter account type (e.g., Savings/Current): ");

String accType = [Link]();

[Link]("Enter initial balance: ");

double initialBalance = [Link]();

[Link](name, accNumber, accType, initialBalance);

[Link]("\nEnter amount to deposit: ");

double depositAmount = [Link]();


[Link](depositAmount);

[Link]("Enter amount to withdraw: ");

double withdrawAmount = [Link]();

[Link](withdrawAmount);

[Link]();

[Link]();

OUTPUT: Enter depositor name: ABCD

Enter account number: 21030125642

Enter account type (e.g., Savings/Current): Savings

Enter initial balance: 50000

Enter amount to deposit: 20000

Deposited: 20000.0

Enter amount to withdraw: 10000

Withdrawn: 10000.0

Depositor Name: ABCD

Current Balance: $60000.0

You might also like