Java
Java
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
int x = sc.nextInt();
int a=0;
int b=0;
int d=0;
while(x>0)
{
int c = x%10;
a=a+c;
x = x/10;
b=b+(c*c);
}
d=(a*a)-b;
System.out.println(d);
}
}
id - int
brand - string
type - string
price - double
This method will take a String parameter along with the other parameter as array of
Car objects.
The method will return array of Cars where the String parameter appears in the type
attribute (with case insensitive search)
This method should be called from main method and display the id of returned objects
in ascending order.
Before calling this method (searchCarType) in the main method, use Scanner object to
read values for four Car objects referring the attributes in the above sequence.
Next call the method searchCarType, write the logic to sort the id in ascending
order (in main method) and display the result.
INPUT:
1
Ferrari ZXi
Sports
2500000
2
Tata Xpnc
Luxury
1000000
3
BMW
Sports
1700000
4
Benz Dz
Luxury
2000000
Luxury
OUTPUT:
2
4
SOLUTION
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
public class Solution {
public static void main(String args[])
{
Car[] c=new Car[4];
Scanner sc=new Scanner(System.in);
for(int i=0;i<c.length;i++)
{
int id=sc.nextInt();sc.nextLine();
String brand=sc.nextLine();
String type=sc.nextLine();
double price=sc.nextDouble();
c[i]=new Car(id,brand,type,price);
}
String str=sc.next() ;
Car result[]=searchCarType(str,c);
for(int f=0;f<result.length;f++)
{
System.out.println(result[f].getId());
}
}
}
for(int p=0;p<result.length;p++)
{
for(int j=p+1;j<result.length;j++) {
if(result[p].getId()>result[j].getId())
{
Car temp=result[p];
result[p]=result[j];
result[j]=temp;
}
}
}
return result;
}
}
class Car
{
private int id;
private String brand;
private String type;
private double price;
this.id = id;
this.brand = brand;
this.type = type;
this.price = price;
}
System.out.println(c[z].getCustomerId()+c[z].getCustomerType()+c[z].getTransac
tionType()+c[z].getPaymentAmount());
}
}
public static void getCustomerBalance(Customer[] c, double adjustment )
{
for(int j=0;j<c.length;j++) {
if(c[j].getTransactionType().equals("discount"))
{
double k=c[j].getPaymentAmount()-(adjustment);
c[j].setPaymentAmount(k);
}
if(c[j].getTransactionType().equals("surcharge") &&
c[j].getCustomerType().equals("corporate"))
{
double m=c[j].getPaymentAmount()+(adjustment);
c[j].setPaymentAmount(m);
}
if(c[j].getTransactionType().equals("surcharge") &&
c[j].getCustomerType().equals("ordinary")) {
double n=c[j].getPaymentAmount()+(adjustment)+100;
c[j].setPaymentAmount(n);
}
}
for(int z=0;z<c.length;z++) {
System.out.println(c[z].getCustomerId()+c[z].getCustomerType()+c[z].getTransac
tionType()+c[z].getPaymentAmount());
}
}
class Customer{
private int customerId;
private String customerType;
private String transactionType;
private double paymentAmount;
public Customer(int customerId, String customerType, String transactionType,
double paymentAmount) {
super();
this.customerId = customerId;
this.customerType = customerType;
this.transactionType = transactionType;
this.paymentAmount = paymentAmount;
}
public int getCustomerId() {
return customerId;
}
public void setCustomerId(int customerId) {
this.customerId = customerId;
}
public String getCustomerType() {
return customerType;
}
public void setCustomerType(String customerType) {
this.customerType = customerType;
}
public String getTransactionType() {
return transactionType;
}
public void setTransactionType(String transactionType) {
this.transactionType = transactionType;
}
public double getPaymentAmount() {
return paymentAmount;
}
public void setPaymentAmount(double paymentAmount) {
this.paymentAmount = paymentAmount;
}
}
4)
import java.util.*;
import java.io.*;
import java.text.*;
import java.text.*;
import java.util.regex.*;
public class Solution {
public static void main(String[] args) {
}
public static Order getHighestOrder(Order[] o) {
Order largestorder=o[0];
for(int j=0;j<o.length;j++) {
if(o[0].getOrderAmount()<o[j].getOrderAmount()) {
largestorder=o[j];
return largestorder;
}
}
class Order{
private int orderId;
private int noOfItems;
private String transactionType;
private int CustomerId;
private double OrderAmount;
public Order(int orderId, int noOfItems, String transactionType, int
customerId, double orderAmount) {
super();
this.orderId = orderId;
this.noOfItems = noOfItems;
this.transactionType = transactionType;
this.CustomerId = customerId;
this.OrderAmount = orderAmount;
}
public int getOrderId() {
return orderId;
}
public void setOrderId(int orderId) {
this.orderId = orderId;
}
public int getNoOfItems() {
return noOfItems;
}
public void setNoOfItems(int noOfItems) {
this.noOfItems = noOfItems;
}
public String getTransactionType() {
return transactionType;
}
public void setTransactionType(String transactionType) {
this.transactionType = transactionType;
}
public int getCustomerId() {
return CustomerId;
}
public void setCustomerId(int customerId) {
CustomerId = customerId;
}
public double getOrderAmount() {
return OrderAmount;
}
}
5)
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
public class Replace {
for(int i=0;i<str.length();i++)
{
char c=str.toLowerCase().charAt(i);
if(c=='i')
{
s=str.replace('i','m');
}
}
System.out.println(s);
System.out.println(p);
}
}
6) "Create class Book with below attributes:
id - int
title - String
author - String
price - double
This method should be called from main method and display values of returned objects as shared in the
sample.
Before calling this method, use Scanner object to read values for four Book objects referring attributes
in the above sequence.
Input:
1
hello
writer1
50
2
cup
writer2
55
3
Planet
writer3
45
4
India
writer4
40
If above issue is observed, then it is suggested to add one more explicit call to nextLine() after reading
numeric value."
SOLUTION
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
for(int i = 0;i<books.length;i++)
{
int id = sc.nextInt();sc.nextLine();
String title = sc.nextLine();
String author = sc.nextLine();
sortBooksByPrice(books);
for(Book b:books)
{
System.out.println(b.getId()+" "+b.getTitle()+ " " + b.getAuthor()+ " "+ b.getPrice());
}
for(int i = 0;i<books.length;i++)
{
for(int j = 0;j<i;j++)
{
if(books[i].getPrice() < books[j].getPrice())
{
Book tmp = books[i];
books[i] = books[j];
books[j] = tmp;
}
}
}
}
}
class Book
{
Method will read input number (without decimals) and print the maximum digit in that number.
8656
output:
8
SOLUTION
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
int x = sc.nextInt();
int a = 0;
while(x>0)
{
int c = x%10;
if(a<c) a = c;
x = x/10;
}
System.out.println(a);
}
}
8)
"Define main method in Solution class.
The program will read a String value. It will calculate total number of vowels in that string and print the
answer.
Sample input:
internal
Output:
Sample input:
Apple
Output:
SOLUTION
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
int code = 0;
for(int i = 0;i<str.length();i++)
{
char c = str.toLowerCase().charAt(i);
if(c=='a'||c=='e'||c=='i'||c=='o'||c=='u')
code++;
}
System.out.println(code);
}
}
9)
"Create class Document with below attributes
id - int
title - String
folderName - String
pages - int
This method will take array of Document objects and return another array with Document objects
which has odd number of pages.
This method should be called from main method and display values of returned objects as shared in the
sample (in ascending order of id attribute).
Before calling this method, use Scanner object to read values for four Document objects referring
attributes in the above sequence.
Input:
1
resume
personal
50
2
question1
exams
55
3
question2
exams
45
4
India
misc
40
If above issue is observed, then it is suggested to add one more explicit call to nextLine() after reading
numeric value."
SOLUTION
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
for(int i = 0;i<docs.length;i++)
{
int id = sc.nextInt();sc.nextLine();
String title = sc.nextLine();
String folderName = sc.nextLine();
int pages = sc.nextInt();
for(Document doc:newDocs)
{
System.out.println(doc.getId() + " " + doc.getTitle() + " " + doc.getFolderName() + " " +
doc.getPages());
}
}
public static Document[] docsWithOddPages(Document[] docs)
{
Document[] newDocs = null;
int n = 0;
for(Document doc:docs)
{
if(doc.getPages()%2 == 1) n++;
}
n = 0;
for(Document doc:docs)
{
if(doc.getPages()%2 == 1) newDocs[n++] = doc;
}
for(int i = 0;i<newDocs.length;i++)
{
for(int j = 0;j<i;j++)
{
if(newDocs[i].getId() < newDocs[j].getId())
{
Document tmp = newDocs[i];
newDocs[i] = newDocs[j];
newDocs[j] = tmp;
}
}
}
return newDocs;
}
}
class Document
{
private int id;
private String title;
private String folderName;
private int pages;
The program will read a number (without decimal) and print the same in reverse order.
Sample input:
3454
Output
4543
SOLUTION
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
int x = sc.nextInt();
String a = "";
while(x>0)
{
int c = x%10;
a=a+c;
x = x/10;
}
System.out.println(a);
}
}
11)
"Write main method in Solution class.
The method will read a String value and print the maximum valued character (as per alphabet and ASCII
sequence).
Input:
HellO
Output:
Important: It is 'l' and not 'O' since ASCII for 'l' is higher than 'O'.
SOLUTION
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
char c='x';
for(int i = 0;i<str.length();i++)
{
if(i == 0)
{
c = str.charAt(i);
}
else
{
if(c<str.charAt(i))
c = str.charAt(i);
}
System.out.println(c);
}
}
12)
"
Create class Document with below attributes
id - int
title - String
folderName - String
pages - int
This method will take array of Document objects and return another document object which will be
combinition of all objects in array.
This method should be called from main method and display values of returned object as shared in the
sample.
Before calling this method, use Scanner object to read values for four Document objects referring
attributes in the above sequence.
Input:
1
resume
personal
50
2
question1
exams
55
3
question2
exams
45
4
India
misc
40
Output:
5
resume#question1#question2#India
personal@exams@exams@misc
190
If above issue is observed, then it is suggested to add one more explicit call to nextLine() after reading
numeric value."
SOLUTION
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
for(int i = 0;i<docs.length;i++)
{
int id = sc.nextInt();sc.nextLine();
String title = sc.nextLine();
String folderName = sc.nextLine();
int pages = sc.nextInt();
docs[i] = new Document(id,title,folderName,pages);
System.out.println(doc.getId());
System.out.println(doc.getTitle());
System.out.println(doc.getFolderName());
System.out.println(doc.getPages());
}
for(int i = 0;i<docs.length;i++)
{
if(id < docs[i].getId()) id = docs[i].getId();
if(i == docs.length - 1)
{
title += docs[i].getTitle();
folderName += docs[i].getFolderName();
}
else
{
title += docs[i].getTitle()+"#";
folderName += docs[i].getFolderName()+"@";
}
pages += docs[i].getPages();
}
id++;
return doc;
}
}
class Document
{
private int id;
private String title;
private String folderName;
private int pages;
123
Output
SOLUTION
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
int x = sc.nextInt();
int a = 0;
while(x>0)
{
int c = x%10;
a=a+c;
x = x/10;
}
System.out.println(a);
}
}
14)
The method will read a String value and print the minimum valued character (as per alphabet and ASCII
sequence).
HellO
Output:
Important: Answer is not 'e' since 'H' has lower ASCII value then 'e' "
SOLUTION
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
char c='x';
for(int i = 0;i<str.length();i++)
{
if(i == 0)
{
c = str.charAt(i);
}
else
{
if(c>str.charAt(i))
c = str.charAt(i);
}
System.out.println(c);
}
}
15)
IF THE GIVEN NUMBER IS POWER OF 2 OR NOT
EG 2^3=8
IF YES PRINT TRUE ELSE FALSE
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
public class Solution {
}
else
{
System.out.println("FALSE");
}
}
16)
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
public class Solution {
public static void main(String args[])
{
Item[] it=new Item[4];
Scanner sc=new Scanner(System.in);
for(int i=0;i<it.length;i++)
{
int itemId=sc.nextInt();sc.nextLine();
String itemName=sc.nextLine();
String category=sc.nextLine();
double price=sc.nextDouble();
it[i]=new Item(itemId,itemName,category,price);
}
String str=sc.next() ;
Item[] result=searchNameByCategory(str,it);
for(int f=0;f<result.length;f++)
{
System.out.println(result[f].getItemId());
}
}
}
for(int p=0;p<result.length;p++)
{
for(int j=p+1;j<result.length;j++) {
if(result[p].getItemId()>result[j].getItemId())
{
Item temp=result[p];
result[p]=result[j];
result[j]=temp;
}
}
}
return result;
}
}
class Item
{
private int itemId;
private String itemName;
private String category;
private double price;
public Item(int itemId, String itemName, String category, double price) {
super();
this.itemId = itemId;
this.itemName = itemName;
this.category = category;
this.price = price;
}
public int getItemId() {
return itemId;
}
public void setItemId(int itemId) {
this.itemId = itemId;
}
public String getItemName() {
return itemName;
}
public void setItemName(String itemName) {
this.itemName = itemName;
}
public String getCategory() {
return category;
}
public void setCategory(String category) {
this.category = category;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
17)
public class ComplexNumber{
//for real and imaginary parts of complex numbers
double real, img;
for(i=0;i<array.length;i++)
{
for(j=i+1;j<array.length;j++)
{
if(array[i]>array[j])
{
int temp;
temp=array[i];
array[i]=array[j];
array[j]=temp;
}
}
}
for(k=0;k<array.length;k++)
{
if((array[k]%2)!=0)
{
arr[p]=array[k];
p++;
}
for(m=0;m<arr.length;m++)
{
System.out.println(arr[m]);
}
}
public static void main(String[] args)
{
int array[]={111,77,88,44,32,11,13,25,44};
sortOddValues(array);
}
}
}
public class Solution {
public static void findSequentialDistance(Point p[])
{
double a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,s;
a=((p[1].getX())-(p[0].getX()));
b=((p[1].getX())-(p[0].getX()));
c=((p[1].getY())-(p[0].getY()));
d=((p[1].getY())-(p[0].getY()));
e=Math.sqrt((a*b)+(c*d));
f=((p[2].getX())-(p[1].getX()));
g=((p[2].getX())-(p[1].getX()));
h=((p[2].getY())-(p[1].getY()));
i=((p[2].getY())-(p[1].getY()));
j=Math.sqrt((f*g)+(h*i));
k=((p[3].getX())-(p[2].getX()));
l=((p[3].getX())-(p[2].getX()));
m=((p[3].getY())-(p[2].getY()));
n=((p[3].getY())-(p[2].getY()));
o=Math.sqrt((k*l)+(m*n));
s=e+j+o;
System.out.println(s);
}
findSequentialDistance(p);
20)
}
}
class Book {
Import java.io.*;
Import java.util.*;
Import java.text.*;
Import java.math.*;
Import java.util.regex.*;
}
for (int i = 0; i< j - 1; i++) {
int min_idx = i;
for (int k = i + 1; k < j; k++)
if (acs[k] <acs[min_idx])
min_idx = k;
}
Return 1;
}
}
22)
//STUDENTS FROM SAME CITY AND SAME MARKS
Import java.io.*;
Import java.util.*;
Import java.text.*;
Import java.math.*;
Import java.util.regex.*;
}
for (i = 0; i<re.length; i++) {
System.out.println(re[i].id + " " + re[i].name + " " + re[i].city +
" " + re[i].marks);
}
class Student {
int id;
String name;
String city;
double marks;
23)
//RETURN VOWEL STRING
Import java.io.*;
Import java.util.*;
Import java.text.*;
Import java.math.*;
Import java.util.regex.*;
25)
import java.util.Scanner;
class CheckEvenOdd
{
public static void main(String args[])
{
int num;
System.out.println("Enter an Integer number:");
class CheckEvenOdd
{
public static void main(String args[])
{
int num;
System.out.println("Enter an Integer number:");
int year;
Scanner scan = new Scanner(System.in);
System.out.println("Enter any Year:");
year = scan.nextInt();
scan.close();
boolean isLeap = false;
if(year % 4 == 0)
{
if( year % 100 == 0)
{
if ( year % 400 == 0)
isLeap = true;
else
isLeap = false;
}
else
isLeap = true;
}
else {
isLeap = false;
}
if(isLeap==true)
System.out.println(year + " is a Leap Year.");
else
System.out.println(year + " is not a Leap Year.");
}
}
28)
import java.util.Scanner;
class PalindromeTest {
public static void main(String args[])
{
String reverseString="";
Scanner scanner = new Scanner(System.in);
if (inputString.equals(reverseString))
System.out.println("Input string is a palindrome.");
else
System.out.println("Input string is not a palindrome.");
}
}
29)
public class JavaExample {
30)
public class Example
{
public void reverseWordInMyString(String str)
{
/* The split() method of String class splits
* a string in several strings based on the
* delimiter passed as an argument to it
*/
String[] words = str.split(" ");
String reversedString = "";
for (int i = 0; i < words.length; i++)
{
String word = words[i];
String reverseWord = "";
for (int j = word.length()-1; j >= 0; j--)
{
/* The charAt() function returns the character
* at the given position in a string
*/
reverseWord = reverseWord + word.charAt(j);
}
reversedString = reversedString + reverseWord + " ";
}
System.out.println(str);
System.out.println(reversedString);
}
public static void main(String[] args)
{
Example obj = new Example();
obj.reverseWordInMyString("Welcome to BeginnersBook");
obj.reverseWordInMyString("This is an easy Java Program");
}
}
32)
import java.util.Scanner;
public class Example
{
public static void main(String args[])
{
int counter, i=0, j=0, temp;
int number[] = new int[100];
Scanner scanner = new Scanner(System.in);
System.out.print("How many elements you want to enter: ");
counter = scanner.nextInt();
33)
import java.util.Scanner;
public class JavaExample
{
public static void main(String[] args)
{
int count, temp;
number = num;
while (number != 0)
{
temp = number % 10;
total = total + temp*temp*temp;
number /= 10;
}
if(total == num)
System.out.println(num + " is an Armstrong number");
else
System.out.println(num + " is not an Armstrong number");
}
}
35)
import java.util.Scanner;
public class GCDExample3 {
}
36)FIBONACCI SERIES
import java.util.Scanner;
public class JavaExample {
int i=1;
while(i<=count)
{
System.out.print(num1+" ");
int sumOfPrevTwo = num1 + num2;
num1 = num2;
num2 = sumOfPrevTwo;
i++;
}
}
}