0% found this document useful (0 votes)
33 views16 pages

Gaurav Saini

The document contains multiple code snippets and objectives related to Java programming assignments. It includes programs to calculate sum of even digits, sum of last digits of numbers, create a student class, generate pins from numbers, create classes for books and authors, search and delete elements from arrays, find the second largest element, calculate row sums of a 2D array, and calculate sums of a jagged array.

Uploaded by

lalshalam123
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)
33 views16 pages

Gaurav Saini

The document contains multiple code snippets and objectives related to Java programming assignments. It includes programs to calculate sum of even digits, sum of last digits of numbers, create a student class, generate pins from numbers, create classes for books and authors, search and delete elements from arrays, find the second largest element, calculate row sums of a 2D array, and calculate sums of a jagged array.

Uploaded by

lalshalam123
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/ 16

Name: Gaurav Saini sec:c Roll no 27

course:MCA

Objective: Write a java program to calculate sum of even digits in a


given 10 digit number.

Codes:

import java.util.Scanner;
class Sum
{
public static void main(String[] args)
{
Scanner s1 = new Scanner(System.in);
System.out.println("Enter the number");
long n=s1.nextLong();
long s=0,rem=0;
while(n>0)
{
rem=n%10;
if(rem%2==0)
{
s=s+rem;
}
n=n/10;
}
System.out.println("Sum of even digits of the numbers is:" +s);
}
}
Name Gaurav saini Roll no. 27 section: c
course:MCA

Objective: Write a java program to calculate the sum of last digits


of two six digit numbers.

Codes:

import java.util.Scanner;
class Test
{
public static void main(String[] args)
{
Scanner s1 = new Scanner(System.in);
System.out.println("Enter first number");
int n=s1.nextInt();
System.out.println("Enter second number");
int m=s1.nextInt();
int s=(n%10)+(m%10);
System.out.println("Sum of last digits of the number is:"+s);
}
}
Name Gaurav Saini Roll no.27 Section:C Course:MCA

Objective:Create a class student having following data members:-


1.Roll.no
2Name
Class has the following methods:-
Void display() : To display the details of students.

Codes:
class Student
{
int roll;
String name;
void display()
{
System.out.println("Roll no. is "+roll);
System.out.println("Name is "+name);
}
Student(int roll,String name)
{
this.roll=roll;
this.name=name;
}
}
class Studentmain
{
public static void main(String[] args)
{
Student s1=new Student(101,"Amit");
Student s2=new Student(102,"Aman");
s1.display ();
s2.display ();
}
}
Name Gaurav saini Roll no. 27 section: C
course:MCA

Objective: WAP In Java to generate a four digit pin from three input
numbers of three digits using for loop.
The pin should be made up of 4-digits
The unit position of pin should be the least of the unit position
of
the 3 input numbers.
The tense position of the pin should be the least of the tense
position of the 3 input numbers
The hundredth position of the pin should be the least of the
hundredth position of three input numbers
The thousands position of pin should be the maximum of all the
digits in the three input numbers

code:

import java.util.Scanner;
public class Pin {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the first three-digit number: ");
int num1 = scanner.nextInt();
System.out.print("Enter the second three-digit number: ");
int num2 = scanner.nextInt();
System.out.print("Enter the third three-digit number: ");
int num3 = scanner.nextInt();
int pin = 0;
int thousands = findMax(num1 / 100, num2 / 100, num3 /
100);
pin += thousands * 1000;
int hundredth = findMin(num1 / 10 % 10, num2 / 10 % 10,
num3 / 10 % 10);
pin += hundredth * 100;
int tense = findMin(num1 % 10, num2 % 10, num3 % 10);
pin += tense * 10;
int units = findMin(num1 % 10, num2 % 10, num3 % 10);
pin += units;
System.out.println("Generated Pin: " + pin);
scanner.close();
}
private static int findMin(int a, int b, int c) {
return (a < b) ? ((a < c) ? a : c) : ((b < c) ? b : c);
}
private static int findMax(int a, int b, int c) {
return (a > b) ? ((a > c) ? a : c) : ((b > c) ? b : c);
}
}
Output is:
Name Gaurav saini Roll no. 27 section: C
course:MCA

Objective: Create a class author with the following information:


member variables:name (String), email (String) and gender(char)
Parameterised constructor: To initialize the variables.
create a class book with the following information.
member variable:Name (String), author (of the author class you have
just created), price (double) and qtyInStock(int)
parameterised constructor: to initialize the variables.
getters and setters method for all the member variables.
In the main method create a book object and print all the details of
the book

Code:

class Author {
private String name;
private String email;
private char gender;
public Author(String name, String email, char gender) {
this.name = name;
this.email = email;
this.gender = gender;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public char getGender() {
return gender;
}
public void setGender(char gender) {
this.gender = gender;
}
}
class Book {
private String name;
private Author author;
private double price;
private int qtyInStock;
public Book(String name, Author author, double price, int
qtyInStock) {
this.name = name;
this.author = author;
this.price = price;
this.qtyInStock = qtyInStock;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Author getAuthor() {
return author;
}
public void setAuthor(Author author) {
this.author = author;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public int getQtyInStock() {
return qtyInStock;
}
public void setQtyInStock(int qtyInStock) {
this.qtyInStock = qtyInStock;
}
}
public class Main {
public static void main(String[] args) {
Author author = new Author("John Doe", "[email protected]",
'M');
Book book = new Book("Sample Book", author, 29.99, 50);
System.out.println("Book Details:");
System.out.println("Name: " + book.getName());
System.out.println("Author: " + book.getAuthor().getName());
System.out.println("Email: " + book.getAuthor().getEmail());
System.out.println("Gender: " + book.getAuthor().getGender());
System.out.println("Price: $" + book.getPrice());
System.out.println("Quantity in Stock: " +
book.getQtyInStock());
}
}

output is
Name Gaurav saini Roll no. 27 section: C
course:MCA

Objective:Write a program to search an element in an array

Code:
import java.util.Scanner;
class search {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = 0, num = 0;
int f = 0;
System.out.println("Enter Size of an Array:");
n = sc.nextInt();
int a[] = new int[n];
System.out.println("Enter array elements:");
for (int i = 0; i < n; i++) {
a[i] = sc.nextInt();
}
System.out.println("Enter the element to be find:");
num = sc.nextInt();
for (int i = 0; i < n; i++) {
if (a[i] == num) {
f++;
}
}
if (f == 1) {
System.out.println("Element Found");
} else
{
System.out.println("Element Not Found");
}
}
}

Output is
Name Gaurav saini Roll no. 27 section: C
course:MCA

Objective:Write a program to find second largest element in an array

Code:
import java.util.Scanner;;
public class search1 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter Size of an array");
int n = sc.nextInt();
int a[] = new int[n];
int temp = 0;
System.out.println("Enter Array Elements:");
for (int i = 0; i < a.length; i++) {
a[i] = sc.nextInt();
}
for (int i = 0; i < a.length; i++) {
for (int j = i + 1; j < a.length; j++) {
if (a[i] > a[j]) {
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
}
System.out.print("Sorted in Acending Order: ");
for (int i = 0; i < a.length; i++) {
System.out.print(a[i] + " ");
}
System.out.println("\nSecond Largest: " + a[(n - 2)]);
}
}

Output:
Name Gaurav saini Roll no. 27 section: C
course:MCA

Objective:Write a program to delete an element in the array

Code:
import java.util.Scanner;
public class delete {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter Size of an array:");
int n = sc.nextInt();
int a[] = new int[n];
int pid = 0;
System.out.println("Enter Array Elements:");
for (int i = 0; i < a.length; i++) {
a[i] = sc.nextInt();
}
while (true) {
System.out.println("Enter Index Position to Delete an element in
Array:");
pid = sc.nextInt();
if (pid < 0 || pid >= n) {
System.out.println("Invalid index position. Please enter a valid
index position.\n");
} else {
break;
}
}
int t = pid;
System.out.println("Entered Array: ");
for (int i = 0; i < n; i++) {
System.out.print(a[i] + " ");
}
for (int i = pid; i < n - 1; i++) {
a[i] = a[i + 1];
}
System.out.println("\nArray after deletion at Index Position a[" + t
+ "]: ");
for (int i = 0; i < n - 1; i++) {
System.out.print(a[i] + " ");
}
}
}
output is:
Name Gaurav saini Roll no. 27 section: C
course:MCA

Objective:Write a program to create 2D array of size 3*3 and


calculate the sum of each row

code:
import java.util.Scanner;
class array {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int sum = 0;
int f = 0;
int a[][] = new int[3][3];
System.out.println("Enter array Elements:");
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
a[i][j] = sc.nextInt();
}
}
System.out.println("Entered Array:");
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
System.out.print(a[i][j] + " ");
}
System.out.println();
}
for (int i = 0; i < 3; i++) {
sum = 0;
for (int j = 0; j < 3; j++) {
sum = sum + a[i][j];
}
System.out.println("Row" + (i + 1) + " sum is: " + sum);
}
}
}
output :
Name Gaurav saini Roll no. 27 section: C
course:MCA

Objective:Write a program to create jagged array and calculate the


sum of each element

code:
import java.util.Scanner;
public class jagged {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int sum = 0;
int x = 0, y = 0, z = 0;
System.out.println("Enter size of columns:");
x = sc.nextInt();
y = sc.nextInt();
z = sc.nextInt();
int a[][] = new int[3][];
a[0] = new int[x];
a[1] = new int[y];
a[2] = new int[z];
System.out.println("Enter array Elements:");
for (int i = 0; i < a.length; i++) {
for (int j = 0; j < a[i].length; j++) {
a[i][j] = sc.nextInt();
}
}
System.out.println("Entered Array :");
for (int i = 0; i < a.length; i++) {
for (int j = 0; j < a[i].length; j++) {
System.out.print(a[i][j] + " ");
}
System.out.println();
}
System.out.println("Enter array Elements:");
for (int i = 0; i < a.length; i++) {
for (int j = 0; j < a[i].length; j++) {
sum = sum + a[i][j];
}
}
System.out.println("Sum is :" + sum);
}
}
output:
Name Gaurav saini Roll no. 27 section: C
course:MCA

Objective:Write a program to calculate the sum of each column of 3*3


matrix

code:
import java.util.Scanner;
class array {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int sum = 0;
int f = 0;
int a[][] = new int[3][3];
System.out.println("Enter Array Elements of size 3x3 :");
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
a[i][j] = sc.nextInt();
}
}
System.out.println("\nEntered Array:");
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
System.out.print(a[i][j] + " ");
}
System.out.println();
}
System.out.println("\nSum of\n\nColumn 1 \tColumn 2 \tColumn 3");
for (int i = 0; i < 3; i++) {
sum = 0;
for (int j = 0; j < 3; j++) {
sum = sum + a[j][i];
}
System.out.print(sum + "\t\t");
}
}
}
output:

You might also like