This tutorial extends Spring MVC beginner tutorial step by step.
Maven project structure
Model class to capture login form details
Step 1: Create a POJO “Login.java” in the package “com.mytutorial.model” under “src/main/java”
…
This tutorial extends Spring MVC beginner tutorial step by step.
Maven project structure
Model class to capture login form details
Step 1: Create a POJO “Login.java” in the package “com.mytutorial.model” under “src/main/java”
…
Many production systems run on Linux systems, especially the Cloud infrastructure (i.e. AWS & GCP). Even Android uses the Linux kernel under the hood. So, it really pays to have a good knowledge of Linux.
Q1. How do you check for open ports in Linux?
A1.
…
Q. Complete the following “TreeProcessingImpl” class ….. so that the unit tests shown below pass? Skeleton Code
|
1 2 3 4 5 6 7 8 |
package com.passing.unittests; import java.util.List; public interface Node<E> { E getValue(); List<Node<E>> getChildren(); } |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
package com.passing.unittests; import java.util.List; public class NodeImpl<E> implements Node<E> { private E value; private List<Node<E>> children; public NodeImpl(E value, List<Node<E>> children) { this.value = value; this.children = children; } public E getValue() { return value; } public List<Node<E>> getChildren() { return children; } } |
|
1 2 3 4 5 6 |
package com.passing.unittests; public interface TreeProcessing<E> { abstract double getAverage(Node<E> node); abstract double getSum(Node<E> node); } |
|
1 2 3 4 5 6 7 8 9 10 11 12 |
package com.passing.unittests; public class TreeProcessingImpl<E> implements TreeProcessing<Double> { public double getAverage(Node<Double> node) { return 0; } public double getSum(Node<Double> node) { return 0; } } |
Unit Tests
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
package com.passing.unittests; import java.util.Arrays; import java.util.List; import org.junit.Assert; import org.junit.Before; import org.junit.Test; public class TreeProcessingTest { private TreeProcessing<Double> tp = null; private Node<Double> numberRootNode; @Before public void init() { tp = new TreeProcessingImpl<Double>(); Node<Double> lvl2a = new NodeImpl<Double>(6.0, null); Node<Double> lvl2b = new NodeImpl<Double>(7.0, null); Node<Double> lvl2c = new NodeImpl<Double>(8.0, null); @SuppressWarnings("unchecked") List<Node<Double>> lvl2Children = Arrays.asList(lvl2a, lvl2b, lvl2c); Node<Double> lvl1a = new NodeImpl<Double>(5.0, lvl2Children); Node<Double> lvl1b = new NodeImpl<Double>(9.0, null); @SuppressWarnings("unchecked") List<Node<Double>> lvl1Children = Arrays.asList(lvl1a, lvl1b); numberRootNode = new NodeImpl<Double>(7.0, lvl1Children); } @Test public void testGetAverage() { Assert.assertEquals("Wrong", (6.0+7.0+8.0+5.0+9.0+7.0)/6, tp.getAverage(numberRootNode), 0.01); } @Test public void testSum() { Assert.assertEquals("Wrong", (6.0+7.0+8.0+5.0+9.0+7.0), tp.getSum(numberRootNode), 0.01); } } |
A. … Read more ›...
Q. How does hibernate support lazy loading? A. Hibernate uses a proxy object to support lazy loading. Basically as soon as you reference a child or lookup object via the accessor/getter methods, if the linked entity is not in the session cache (i.e. the first-level cache), … Read more ›...
Step 1: Create a simple Maven project from a command-line. Just press enter for all the prompts.
|
1 |
mvn archetype:generate -DgroupId=com.mytutorial -DartifactId=simple-mocking-test |
Step 2: Import the above “simple-mocking-test” in the file system into eclipse. File –> Import –> “Existing Maven Projects” and then select the “simple-mocking-test”
…
Q. Complete the method “canAttendAll(Collection seminars)” which takes a collection of seminar objects with start and end timestamps, and the method should determine if there is an overlap in seminars events. In other words, if you can attend all the seminars.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Collection; import java.util.Date; public class SeminarWeek { public static Boolean canAttendAll(Collection<Seminar> seminars) { return false; } public static void main(String[] args) throws Exception { SimpleDateFormat sdf = new SimpleDateFormat("y-M-d H:m"); ArrayList<Seminar> seminars = new ArrayList<Seminar>(); seminars.add(new Seminar(sdf.parse("2015-01-13 09:00"), sdf .parse("2015-01-13 16:30"))); seminars.add(new Seminar(sdf.parse("2015-01-12 09:30"), sdf .parse("2015-01-12 17:00"))); seminars.add(new Seminar(sdf.parse("2015-01-14 09:10"), sdf .parse("2015-01-14 16:30"))); System.out.println(SeminarWeek.canAttendAll(seminars)); } } class Seminar { private Date start, end; public Seminar(Date start, Date end) { this.start = start; this.end = end; } public Date getStart() { return this.start; } public Date getEnd() { return this.end; } } |
The above code needs to be saved into...
Q1. What is a Servlet? Is a Servlet inherently multi-threaded? A1. A Servlet is a Java class that runs within a web container in an application server, servicing multiple client requests concurrently forwarded through the server and the web container. The web browser establishes a socket connection to the host...
Continuation of Java architecture interview Q&As – part 1 & 7+ Java architectural patterns interview Q&As – Part 2. Q3. Can you discuss some of the high level architectures you are experienced with? A3. Be prepared for a white board session on architectures, … Read more ›...
7 Things you must know about Java locks and synchronized key word summarizes the key basics. #1. Each Java class and object (i.e. instance of a class) has an intrinsic lock or monitor. Don’t confuse this with explicit lock utility class that were added in Java 1.5, … Read more...
Q01. What is an Azure Storage Account? A01 An Azure storage account contains all of your Azure Storage data objects blobs, files, queues, and tables. … Read more ›...
This question is a very popular white board session question for both Java architects and experienced JEE Developers. You need to draw on your experience to tackle this question as there are no right or wrong answers. These high level diagrams and summary will help you refresh your memory. Q....
NonUniqueObjectException is thrown when there is an object already associated with the session with the same id (primary key) (i.e. a duplicate). It is important to understand the concept of a “detached” object in Hibernate. This is one of the most common errors, understanding why and when this error is thrown will save you time in identifying and fixing this issue.
…
Q01: What are text blocks? A01: Java 15 introduced text blocks feature to declare multi-line strings. Prior to Java 15
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
package com.myorg.hello; public class TextBlocks { public static void main(String[] args) { String sql = " SELECT id, first_nm, last_nm\n" + " FROM employee\n" + " WHERE dept = \"IT\"\n" + " ORDER BY first_nm, last_nm ASC"; String html = " <html>\n" + " <body>\n" + " <p>Hello World!</p>\n" + " </body>\n" + " </html>"; System.out.println(sql); System.out.println("-------------------------------"); System.out.println(html); } } |
Outputs:
|
1 2 3 4 5 6 7 8 9 10 11 |
SELECT id, first_nm, last_nm FROM employee WHERE dept = "IT" ORDER BY first_nm, last_nm ASC ------------------------------- <html> <body> <p>Hello World!</p> </body> </html> |
Java 15+ The Pre Java 15 approach is harder to read & … Read more ›...