100% found this document useful (1 vote)
16K views

Xplore Java HandsOn

The document describes code for calculating interest on bank accounts. It defines an Account class with fields for ID, balance, and interest rate. The main method gets input for an account, calculates interest over a number of years using a calculateInterest method, and outputs the result. The calculateInterest method multiplies the initial balance by 1 plus the interest rate over the number of years to calculate the final balance.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
16K views

Xplore Java HandsOn

The document describes code for calculating interest on bank accounts. It defines an Account class with fields for ID, balance, and interest rate. The main method gets input for an account, calculates interest over a number of years using a calculateInterest method, and outputs the result. The calculateInterest method multiplies the initial balance by 1 plus the interest rate over the number of years to calculate the final balance.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

1

Numeric Computation- Hands on 1

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[] ) throws Exception {
int a;
double b,c;

Scanner sc = new Scanner(System.in);


a = sc.nextInt();
b = sc.nextDouble();
c = sc.nextDouble();

Account account = new Account(a, b, c);

int noOfYear;
noOfYear = sc.nextInt();

double answer = calculateInterest(account, noOfYear);


System.out.format("%.3f",answer);
}

public static double calculateInterest(Account account, int noOfYear) {


double temp = noOfYear * account.getInterestRate() / 100;
return (account.getBalance() * (account.getInterestRate()+temp) / 100);
}
}

class Account {
private int id;
private double balance;
private double interestRate;
2

Account(int id, double balance, double interestRate) {


this.id = id;
this.balance = balance;
this.interestRate = interestRate;
}

public int getId() {


return this.id;
}
public void setId(int id) {
this.id = id;
}

public double getBalance() {


return this.balance;
}
public void setBalance(double balance) {
this.balance = balance;
}

public double getInterestRate() {


return this.interestRate;
}
public void setInterestRate(double interestRate) {
this.interestRate = interestRate;
}
}
3

Classes and Objects - Hands on 1

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[] ) throws Exception {
int x1,y1,x2,y2;
Scanner scn=new Scanner(System.in);
x1=scn.nextInt();
y1=scn.nextInt();
x2=scn.nextInt();
y2=scn.nextInt();
Point p1=new Point(x1, y1);
Point p2=new Point(x2, y2);
double distance=findDistance(p1, p2);
System.out.format("%.3f",distance);
}

public static double findDistance(Point p1, Point p2)


{
double distance=Math.sqrt((p2.x-p1.x)*(p2.x-p1.x)+(p2.y-p1.y)*(p2.y-p1.y));
return distance;
}
}

class Point
{
int x,y;
Point(int x,int y)
{
this.x=x;
this.y=y;
}
}
4

Conditional Operands - Hands on 1


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[] ) throws Exception {
/* Enter your code here. Read input from STDIN. Print output to STDOUT */
double x1,y1,x2,y2,x3,y3;
Scanner scn=new Scanner(System.in);
x1=scn.nextDouble();
y1=scn.nextDouble();
x2=scn.nextDouble();
y2=scn.nextDouble();
x3=scn.nextDouble();
y3=scn.nextDouble();
Point p1=new Point(x1, y1);
Point p2=new Point(x2, y2);
Point p3=new Point(x3, y3);
Point highest=pointWithHighestOriginDistance(p1, p2, p3);
System.out.format("%.1f \n",highest.x);
System.out.format("%.1f",highest.y);
}
public static Point pointWithHighestOriginDistance(Point p1, Point p2, Point p3)
{
double d1=Math.sqrt(p1.x*p1.x+p1.y*p1.y);
double d2=Math.sqrt(p2.x*p2.x+p2.y*p2.y);
double d3=Math.sqrt(p3.x*p3.x+p3.y*p3.y);
return d1>d2?(d1>d3?p1:p3):(d2>d3?p2:p3);
}
}

class Point
{
double x,y;
5

Point(double x, double y)
{
this.x=x;
this.y=y;
}
}
6

Java Iterations - Hands on 1


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[] ) throws Exception {
/* Enter your code here. Read input from STDIN. Print output to STDOUT */
String str;
Scanner scn=new Scanner(System.in);
str=scn.next();
int[] values=new int[str.length()];
for(int i=0;i<str.length();i++)
{
values[i]=(int)(str.charAt(i));
}
int min=values[0];
for(int i=0;i<values.length;i++)
{
if(values[i]<=min)
min=values[i];
}
char c=(char)min;
System.out.print(c);
}
}
7

Java Iterations - Hands on 2


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[] ) throws Exception {
/* Enter your code here. Read input from STDIN. Print output to STDOUT */
Scanner scn=new Scanner(System.in);
int []num=new int[5];
for(int i=0;i<5;i++)
{
num[i]=scn.nextInt();
String res=factorial(num[i]);
System.out.println(res);

public static String factorial(int n)


{
BigInteger fact=new BigInteger("1");
for(int i=1;i<=n;i++){
fact=fact.multiply(new BigInteger(i+""));
}
return fact.toString();
}
}
8

Java Arrays - Hands on 1


Problem 1:
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[] ) throws Exception {
/* Enter your code here. Read input from STDIN. Print output to STDOUT */
Scanner scn=new Scanner(System.in);
Document[] docsArray=new Document[4];
Document[] res=new Document[4];
for(int i=0;i<docsArray.length;i++)
{
docsArray[i]=new Document();
res[i]=new Document();
}
for(int i=0;i<docsArray.length;i++)
{
docsArray[i].id=scn.nextInt();
docsArray[i].title=scn.next();
docsArray[i].folderName=scn.next();
docsArray[i].pages=scn.nextInt();
}
res= docsWithOddPages(docsArray);
for(int i=0;i<res.length;i++)
{
if(res[i].title!=null)
System.out.println(res[i].id+" "+res[i].title+" "+res[i].folderName+" "+res[i].pages);
}
}
public static Document[] docsWithOddPages(Document[]docsArray){
Document[] oddDocs=new Document[4];
for(int i=0;i<docsArray.length;i++)
{
9

oddDocs[i]=new Document();
}
int k=0;
for(int i=0;i<docsArray.length;i++)
{
if(docsArray[i].pages%2!=0)
{
oddDocs[k++]=docsArray[i];
}
}
return oddDocs;
}
}

class Document
{
int id,pages;
String title,folderName;
}

Problem 2:

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[] ) throws Exception {
/* Enter your code here. Read input from STDIN. Print output to STDOUT */
Scanner scn=new Scanner(System.in);
Book[] booksArray=new Book[4];
Book[] sorted=new Book[4];
for(int i=0;i<booksArray.length;i++)
{
booksArray[i]=new Book();
sorted[i]=new Book();
10

}
for(int i=0;i<booksArray.length;i++)
{
booksArray[i].id=scn.nextInt();
booksArray[i].title=scn.next();
booksArray[i].author=scn.next();
booksArray[i].price=scn.nextDouble();
}
sorted=sortBooksByPrice(booksArray);
for(int i=0;i<sorted.length;i++)
{
System.out.println(sorted[i].id+" "+sorted[i].title+" "+sorted[i].author+"
"+sorted[i].price);
}
}
public static Book[] sortBooksByPrice(Book[]booksArray){
int n=booksArray.length;
for(int i=0;i<n-1;i++)
{
for(int j=0;j<n-i-1;j++)
{
if(booksArray[j].price>booksArray[j+1].price)
{
Book temp=booksArray[j];
booksArray[j]=booksArray[j+1];
booksArray[j+1]=temp;
}
}
}
return booksArray;
}
}

class Book
{
int id;
String title,author;
double price;
}
11

Java Arrays - Hands on 2


Problem 1:

import java.util.Scanner;

public class Solution {


public static void main(String args[] ) throws Exception {
/* Do not alter code in main method */
Shirt[] shirts = new Shirt[5];

Scanner sc = new Scanner(System.in);

for(int i = 0;i<5;i++)
{
int tag = sc.nextInt();sc.nextLine();
String brand = sc.nextLine();
double price = sc.nextDouble();sc.nextLine();
char g = sc.nextLine().charAt(0);
shirts[i] = new Shirt(tag,brand,price,g);
}

double price = sc.nextDouble();

for(Shirt s: shirts)
{
System.out.println(getDiscountPrice(s));
}

Shirt[] result = getShirtWithMoreThanSpecificPrice(shirts,price);

for(Shirt s: result)
{
if(s.getTag()!=0)
System.out.println(s.getTag()+" "+s.getPrice()+ " " + s.getBrand());
}
12

/* implement your methods here*/


public static double getDiscountPrice(Shirt s)
{
double discount;
if(s.gender=='m')
discount=10;
else if(s.gender=='f')
discount=20;
else if(s.gender=='u')
discount=30;
else
discount=0;
return s.price-(discount*s.price)/100;

}
public static Shirt[] getShirtWithMoreThanSpecificPrice(Shirt [] shirts, double price)
{
Shirt[] res = new Shirt[5];
for(int i=0;i<res.length;i++)
res[i]=new Shirt(0,"",0,'c');// taking sample to avoid null pointer exception
int j=0;
for(int i=0;i<shirts.length;i++)
{
if(shirts[i].price>price)
{
res[j++]= new Shirt(shirts[i].tag,shirts[i].brand,shirts[i].price,shirts[i].gender);
}
}
return res;
}
}

class Shirt
{
//define the class as per details shared in the question
int tag;
String brand;
13

double price;
char gender;
Shirt(int tag, String brand, double price, char gender)
{
this.tag=tag;
this.brand=brand;
this.price=price;
this.gender=gender;
}
public int getTag()
{
return this.tag;
}
public void setTag(int tag)
{
this.tag=tag;
}
public String getBrand()
{
return this.brand;
}
public void setBrand(String brand)
{
this.brand=brand;
}
public double getPrice()
{
return this.price;
}
public void setPrice(double price)
{
this.price=price;
}
public char getGender()
{
return this.gender;
}
public void setGender(char gender)
{
14

this.gender=gender;
}
}

Problem 2:

import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
import java.util.*;

public class Solution {


public static void main(String args[] ) throws Exception {
/* Enter your code here. Read input from STDIN. Print output to STDOUT */
Scanner scn=new Scanner(System.in);
Book[] booksArray=new Book[4];
Book[] res=new Book[4];
for(int i=0;i<booksArray.length;i++)
{
booksArray[i]=new Book();
res[i]=new Book();
}
for(int i = 0;i<4;i++)
{
booksArray[i].id = scn.nextInt();scn.nextLine();
booksArray[i].title = scn.nextLine();
booksArray[i].author = scn.nextLine();
booksArray[i].price = scn.nextDouble();
}
String value=scn.next();
res=searchTitle(value, booksArray);
int [] matchedId=new int[4];
int j=0;
for(int i=0;i<res.length;i++)
{
if(res[i].id!=0)
15

{
matchedId[j++]=res[i].id;
}
}
Arrays.sort(matchedId);
for(int i=0;i<matchedId.length;i++)
{
if(matchedId[i]!=0)
System.out.println(matchedId[i]);
}

}
public static Book[] searchTitle(String value, Book[] books)
{
int k=0;
Book[] matching=new Book[4];
for(int i=0;i<matching.length;i++)
matching[i]=new Book();
for(int i=0;i<books.length;i++)
{
String val=value.toLowerCase();
String bookTitle=books[i].title.toLowerCase();
if(bookTitle.contains(val))
{
matching[k++]=books[i];
}
}
return matching;
}
}

class Book
{
int id;
String title;
String author;
double price;
public int getId()
{
16

return this.id;
}
public void setId(int id)
{
this.id=id;
}
public String getTitle()
{
return this.title;
}
public void setTitle(String title)
{
this.title=title;
}
public String getAuthor()
{
return this.author;
}
public void setAuthor(String author)
{
this.author=author;
}
public double getPrice()
{
return this.price;
}
public void setPrice(double price)
{
this.price=price;
}

You might also like