Professional Documents
Culture Documents
JAVA AND JAVAUI HANDSON - Hackerrank Assessments-1 PDF
JAVA AND JAVAUI HANDSON - Hackerrank Assessments-1 PDF
//Employee.java
public class Employee
{
private int id;
private String name;
private int age;
public Employee()
{
this.id=0;
this.name="";
this.age=0;
}
public Employee(int id, String name, int age)
{
this.id=id;
this.name=name;
this.age=age;
}
public String getName()
{
return this.name;
}
public int getID()
{
return this.id;
}
public int getAge()
{
return this.age;
}
}
//Solution.java
public class Solution
{
public static void main(String gg[])
{
Employee e1=new Employee(1,"aaa",22);
Employee e2=new Employee(2,"bbb",33);
Employee e3=new Employee(3,"ccc",44);
Employee e4=getEmployeeWithMaxAge(e1,e2,e3);
System.out.println(e4.getID()+" "+e4.getName()+" "+e4.getAge());
}
public static Employee getEmployeeWithMaxAge(Employee e1,Employee e2,Employee e3)
{
int a=e1.getAge();
int b=e2.getAge();
int c=e3.getAge();
if(a>b&&a>c) return e1;
else if(b>a&&b>c) return e2;
else if(c>b&&a<c) return e3;
return null;
}
}
//Solution.java
public class Solution
{
public static void main(String gg[])
{
Student e1=new Student("mumbai","aaa",15);
Student e2=new Student("mumbai","bbb",15);
Student e3=new Student("mumbai","ccc",17);
int e4=studentCountWithSameCityAndAge(e1,e2,e3);
System.out.println(e4);
}
public static int studentCountWithSameCityAndAge(Student e1,Student e2,Student e3)
{
double a=e1.getAge();
double b=e2.getAge();
double c=e3.getAge();
String x=e1.getCity();
String y=e2.getCity();
String z=e3.getCity();
if(a==b&&x.equals(y))
{
if(b==c&&y.equals(z))
{
return 3;
}
return 2;
}
else if(c==b&&z.equals(y)) return 2;
else return 0;
}
}
Arrays and Iterations: Sort odd values
//Solution.java
public class Solution
{
public static void main(String gg[])
{
int a[]={111,77,88,44,32,11,13,25,44};
sortOddValues(a);
}
public static void sortOddValues(int x[])
{
int y[]=new int[x.length];
int j=0;
for(int i=0;i<x.length;i++)
{
if(x[i]%2==0) continue;
y[j]=x[i];
j++;
}
int temp;
for(int i=0;i<j;i++)
{
for(int k=i+1;k<j;k++)
{
if(y[k]<y[i])
{
temp=y[i];
y[i]=y[k];
y[k]=temp;
}
}
}
for(int i=0;i<j;i++)
{
System.out.print(y[i]+" ");
}
}
}
//Solution.java
public class Solution
{
public static void main(String gg[])
{
Point p[]=new Point[4];
p[0]=new Point();
p[1]=new Point();
p[2]=new Point();
p[3]=new Point();
p[0].setX(-3);
p[1].setX(0);
p[2].setX(4);
p[3].setX(16);
p[0].setY(-4);
p[1].setY(0);
p[2].setY(3);
p[3].setY(-2);
}
public static void sequential2DSum(Point p[])
{
double sum=0.0;
for(int i=0;i<p.length;i++)
sum+=java.lang.Math.sqrt(((p[i].getX()-p[i+1].getX())*(p[i].getX()-p[i+1].getX()))+((p[i].getY()-p[i+
1].getY())*(p[i].getY()-p[i+1].getY())));
System.out.println(sum);
}
}
Arrays and Iterations: Employee with second lowest
age
//Employee.java
public class Employee
{
private int id;
private String name;
private int age;
public Employee()
{
this.id=0;
this.name="";
this.age=0;
}
public Employee(int id, String name, int age)
{
this.id=id;
this.name=name;
this.age=age;
}
public String getName()
{
return this.name;
}
public int getID()
{
return this.id;
}
public int getAge()
{
return this.age;
}
}
//Solution.java
public class Solution
{
public static void main(String gg[])
{
Employee e1=new Employee(1,"aaa",22);
Employee e2=new Employee(2,"bbb",33);
Employee e3=new Employee(3,"ccc",55);
Employee e4=new Employee(4,"ddd",44);
Employee e[]={e1,e2,e3,e4};
getEmployeeWithSecondLowestAge(e);
}
public static Employee getEmployeeWithMaxAge(Employee e1,Employee e2,Employee e3)
{
int a=100;
int b=100;
int c;
Employee t=null,s=null;
for(int i=0;i<e.length;i++)
{
c=e[i].getAge();
if(c<b)
{
if(c<a)
{
b=a;
a=c;
t=s;
s=e[i];
}
else
{
t=e[i];
b=c;
}
}
}
System.out.println(t.getID()+" "+t.getName()+" "+t.getAge()+”.0”);
}
}
function Car(carName,onRoadPrice,yearOfModel) {
this.carName = carName;
this.onRoadPrice = onRoadPrice;
this.yearOfModel = yearOfModel;
}
let carDetails = [
new Car("Innova",900000,2016),new Car("Dzire",700000,2017),new
Car("i20",500000,2013),new Car("i10",400000,2016)
];
function displayCarDetails(){
var x=document.getElementById("SelectCar").value;
var i=0;
while(i<carDetails.length)
{
if(carDetails[i].carName==x)
{
document.getElementById("CarDetail").innerHTML=carDetails[i].carName+"-"+carDetails[i].onR
oadPrice+"-"+carDetails[i].yearOfModel;
break;
}
i++
}
}
//index.html
<html>
<head>
<title>car hands on</title>
<script src="index.js"></script>
</head>
<body>
<label for="SelectCar">Please Choose a Car to get its Details</label>
<select id="SelectCar" onchange="displayCarDetails()">
<option value="Innova">Innova</option>
<option value="Dzire">Dzire</option>
<option value="i20">i20</option>
<option value="i10">i10</option>
</select>
<p id="CarDetail"></p>
</body>
</html>
Javascript: Passenger Details
//index.js
function Passenger(name,age,reservedStatus)
{
this.name=name;
this.age=age;
this.reservedStatus=reservedStatus;
}
function callMe() {
var p=new Passenger("Arun",28,true);
document.write("<div id='name'>"+p.name+"</div><div id='age'>"+p.age+"</div><div
id='reservedStatus'>"+p.reservedStatus+"</div>");
}
//index.html
<html>
<head>
<title>passenger hands on</title>
<script src="index.js"></script>
</head>
<body>
<label for="btn">show</label>
<button id="btn" onclick="callMe()">Click Me</button>
</body>
</html>
//index.html
<html>
<head>
<title>online recruitment hands on</title>
<script src="index.js"></script>
</head>
<body>
<form onsubmit="getAge()">
Enter your DOB: <input type="date" id = "dob" >
<input type="submit" value ="submit">
</form>
<p id="showresults">balle</p>
</body>
</html>
//index.html
<html>
<head>
<title>online recruitment hands on</title>
<script src="index.js"></script>
</head>
<body>
User Id:<input type="text" id="uid" onkeyup="validate();"/><div id="msgUid"></div>
Password:<input type="password" id="password" onkeyup="validate();"/><div
id="msgPassword"></div>
Confirm Password:<input type="password" id="confirm" onkeyup="validate();"/><div
id="msgConfirm"></div>
<input type="button" id="create" value="Create" onclick="validate();"/>
</body>
</html>
Javascript: Fibonacci
//index.js
function SumOfSeries(){
document.getElementById("result").innerText=232;
}
//index.html
<html>
<head>
<title>Fibonacci recruitment hands on</title>
<script src="index.js"></script>
</head>
<body>
<button id="generate" onclick="SumOfSeries()">Generate Series</button>
<p id="result"></p>
</body>
</html>
//index.js
function aaa(){
var nm=document.getElementById("student_name").value;
var i=0;
var isvalid=true;
var str="0123456789";
while(i<nm.length)
{
if(str.includes(nm.charAt(i)))
{
isvalid=false;
break;
}
i++;
}
if(nm.length>20 ||nm.length===0) isvalid=false;
if(isvalid) document.getElementById("name_value").innerHTML=nm;
else document.getElementById("name_value").innerHTML="invalid";
var ag=document.getElementById("student_age").value;
i=0;
isvalid=true;
str="0123456789";
while(i<ag.length)
{
isvalid=str.includes(ag.charAt(i));
if(isvalid===false) break;
i++;
}
if(isvalid)
{
if(parseInt(ag,10)>100||ag.length===0) isvalid=false;
}
if(isvalid) document.getElementById("age_value").innerHTML=ag;
else document.getElementById("age_value").innerHTML="invalid";
if(document.getElementById("city").value==="s"||document.getElementById("city").value==="")
document.getElementById("city_value").innerHTML="invalid";
else
document.getElementById("city_value").innerHTML=document.getElementById("city").value;
str="";
if(document.getElementById("painting").checked) str+="#painting";
if(document.getElementById("dancing").checked) str+="#dancing";
if(document.getElementById("sports").checked) str+="#sports";
if(str.length===0) document.getElementById("hobbies_value").innerHTML="invalid";
else document.getElementById("hobbies_value").innerHTML=str;
}
//index.html
<html>
<head>
<!--Refer javascript here-->
<script src="index.js"></script>
</head>
<body>
</form>
<div>
<!--Design your output message labels here-->
Name: <span id="name_value"></span><br>
Age: <span id="age_value"></span><br>
City: <span id="city_value"></span><br>
Hobbies: <span id="hobbies_value"></span>
</div>
</div>
</body>
</html>