[Solved] java primitive type array object or not?

In Java you only have primitive or references to objects.* int[] arr = { 1, 2 }; int i = arr[0]; assert arr instanceof Object; // true arr is a reference to an array which has int as a element. how it was achieved? int[] has it’s own class int[].class and your arr is an … Read more

[Solved] Polymorphism java 8

public int h = 44; public int getH() { System.out.println(“Beta “+h); // print line 1 return h; } public static void main(String[] args) { Baap b = new Beta(); System.out.println(b.h+” “+b.getH()); // print line 2 Beta bb = (Beta) b; System.out.println(bb.h+” “+bb.getH()); // print line 3 } In your first print statement in your main … Read more

[Solved] Java stream reduce to map

Thanks @HadiJ for answer Map<String,Integer> result = Arrays.stream(str) .reduce(new HashMap<>(), (hashMap, e) -> { hashMap.merge(e, 1, Integer::sum); return hashMap; }, (m, m2) -> { m.putAll(m2); return m; } ); solved Java stream reduce to map

[Solved] show 0 as prefix if value is less than 9

As you seem to have strings that need to be (optionally) padded with zeros, you can use a different approach than generally used to pad integers: public String addPadding(int length, String text) { StringBuilder sb = new StringBuilder(); // First, add (length – ‘length of text’) number of ‘0’ for (int i = length – … Read more

[Solved] Creating an object from a class [closed]

what does each dictionary mean technically? The first Dictionary indicates that the type of the variable is Dictionary. Since you’re creating a new dictionary, you call the Dictionary class’ constructor i.e. Dictionary(). You can think of the new keyword as a word that you need to write in order to call any constructor. That said, … Read more

[Solved] How to formulate this logic using only if statement?

Lets see: The first five letters or numbers are included in the minimum charge; there is a $4 charge for each additional character. In pseudo-code: if sign-length <= 5: charge stays the same else: charge = charge + (sign-length – 5) * rate-per-character Should get you going … solved How to formulate this logic using … Read more

[Solved] Java :- String search in proximity manner

Here is a very naive way without regex. public class NotElegant { public static void main(String[] args){ String text = “doctors found many cancer related chest problems in japan during second world war.”; String term = “cancer problems”; System.out.println(getWordsNearEachOther(text,term,3)); } public static String getWordsNearEachOther(String text, String term, int distance){ String word1= term.split(” “)[0]; String word2= … Read more

[Solved] Call an array from one method to another method [closed]

Here’s a text (comment) illustrated explanation (both the question and the answer): public Object[] methodA() { // We are method A // In which we create an array Object[] someArrayCreatedInMethodA = new Object[10]; // And we can returned someArrayCreatedInMethodA return someArrayCreatedInMethodA; } public void methodB() { // Here we are inside another method B // … Read more

[Solved] How can we convert a string to a user defined object in java [closed]

You want to transfer a String into a Treecreat? You should give Treecreat a constructor like this. public Treecreat(String yourString){ this.myString = yourString; } So the String is in Treecreat and you can work with it in your method, with call the method in this way: add(new Treecreat(data). solved How can we convert a string … Read more

[Solved] Java : Summing the N series fails due to timeout

Finally it got solved. take a look. public class Solution { static int mod = 1000000007; static int summingSeries(long n) { return (int)(((n % mod) * (n % mod)) % mod); } private static final Scanner scanner = new Scanner(System.in); public static void main(String[] args) throws IOException { BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(System.getenv(“OUTPUT_PATH”))); int … Read more

[Solved] What am I missing in my code? JAVA [closed]

for (int i = 0; i < str.length(); i++) { if (str.charAt(i) == ‘ ‘ || str.charAt(i) == ‘\t’ || str.charAt(i) == ‘\n’) { // end of token, check if key word String currentWord = s.toString(); boolean isKeyword = false for (int j = 0; j < keywords.length; j++) { if (currentWord.equalsIgnoreCase(keywords[j])) { isKeyword = … Read more