0% found this document useful (1 vote)
845 views15 pages

JAVA AND JAVAUI HANDSON - Hackerrank Assessments-1 PDF

The document contains code examples demonstrating the use of conditional operators and methods in Java. Specifically, it shows: 1) An Employee class with methods to get employee ID, name and age. 2) A Solution class with a getEmployeeWithMaxAge method that returns the employee with the maximum age out of three employees. 3) Additional code examples using conditional operators to find students with the same age and city, sort odd values in an array, swap array values, and calculate the sequential 2D sum of Point objects.

Uploaded by

tkkhhaarree
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 (1 vote)
845 views15 pages

JAVA AND JAVAUI HANDSON - Hackerrank Assessments-1 PDF

The document contains code examples demonstrating the use of conditional operators and methods in Java. Specifically, it shows: 1) An Employee class with methods to get employee ID, name and age. 2) A Solution class with a getEmployeeWithMaxAge method that returns the employee with the maximum age out of three employees. 3) Additional code examples using conditional operators to find students with the same age and city, sort odd values in an array, swap array values, and calculate the sequential 2D sum of Point objects.

Uploaded by

tkkhhaarree
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/ 15

Conditional Operators : Employee MAX 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",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;
}
}

Conditional Operators :​Students with same age and


city
//Student.java
public class Student
{
private String city;
private String name;
private double age;
public Student()
{
this.city="";
this.name="";
this.age=0;
}
public Student(String city, String name, double age)
{
this.city=city;
this.name=name;
this.age=age;
}
public String getName()
{
return this.name;
}
public String getCity()
{
return this.city;
}
public double getAge()
{
return this.age;
}
}

//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]+" ");
}
}
}

Arrays and Iterations: ​Swap values


​ //Solution.java
public class Solution
{
public static void main(String gg[])
{
int a[]={111,77,88,44,32,11,13,25,44};
swapValues(a);
}
public static void swapValues(int x[])
{
int temp;
for(int i=0;i<x.length-1;i+=2)
{
temp=x[i];
x[i]=x[i+1];
x[(i+1)]=temp;
}
for(int i=0;i<x.length;i++)
{
System.out.print(x[i]+" ");
}
}
}

Arrays and Iterations: ​Sequential 2D sum


//Point.java
public class Point
{
private double x;
private double y;
public Point()
{
this.x=0.0;
this.y=0.0;
}
public double getX()
{
return this.x;
}
public double getY()
{
return this.y;
}
public setX(double x)
{
this.x=x;
}
public setY(double y)
{
this.y=y;
}
}

//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”);
}
}

Javascript: Car Dealer


//index.js

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>

Javascript: online recruitment


//index.js
function getAge() {
var d=parseInt(document.getElementById("dob").value.substring(0,4));
alert(d)
if(d>=2019) document.getElementById("showresults").innerText="Wrong date!!";
else document.getElementById("showresults").innerText="You are "+(2019-d)+" year(s) old!!";
}

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

Javascript: User Registration


//index.js
function validate(){
var a=document.getElementById("uid");
var b=document.getElementById("password");
var c=document.getElementById("confirm");
if(b.value!=c.value){
document.getElementById("msgUid").innerHTML="Invalid Entry";
document.getElementById("msgPassword").innerHTML="Invalid Entry";
document.getElementById("msgConfirm").innerHTML="Invalid Entry";
}
else{
if(a.value===null ||a.value==="") document.getElementById("msgUid").innerHTML="Invalid
Entry";
else document.getElementById("msgUid").innerHTML="OK!";
if(b.value===null ||b.value==="")
document.getElementById("msgPassword").innerHTML="Invalid Entry";
else document.getElementById("msgPassword").innerHTML="OK!";
if(c.value===null ||c.value==="") document.getElementById("msgConfirm").innerHTML="Invalid
Entry";
else document.getElementById("msgConfirm").innerHTML="OK!";
}
}

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

User Interface - Code Assessment (Hackerrank)

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

<div id="body" style="background-color:pink">

<form name="my_form" onsubmit="aaa()">


<br><br>
<!--Design all html user controls here-->
<span id="name_label" style="background-color:yellow">Student Name</span> <input
style="background-color:yellow" type="text" id="student_name" name="student_name" ><br>
<span id="age_label" style="background-color:yellow">Age</span> <input
style="background-color:yellow" type="text" id="student_age" name="student_age" ><br>
<span>Gender</span> <input type="radio" name="gender" value="male" checked>M
<input type="radio" name="gender" value="female">F<br>
<span>City</span> <select name="city" id="city"><option
value="s">Select</option><option value="Delhi">Delhi</option><option
value="Mumbai">Mumbai</option><option value="Kolkata">Kolkata</option><option
value="Chennai">Chennai</option></select><br>
<span>Hobby</span> <input type="checkbox" id="painting" name="painting"
value="painting" >Painting <input id="dancing" type="checkbox" name="dancing"
value="dancing" >Dancing <input id="sports" type="checkbox" name="sports" value="sports"
>Sports <br>
<button type="submit" id="student_submit">Submit</button>

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

You might also like