Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

wordsFront challenge

Posted on 2016-08-29
8
Medium Priority
?
156 Views
Last Modified: 2016-08-29
Hi,

I am working on below challenge
http://codingbat.com/prob/p183837

Psedo code:
1. create new array of given n size
2. loop through given array
3. fill the new array with given array
4. return new array
I wrote my code as below

public String[] wordsFront(String[] words, int n) {
 int count=0;
 String[] arr=new String[n];
for(int i=0;i<n;i++){
  arr[i]=words[i];
  
 
}
return arr;
}

Open in new window




I am passing all tests

How to improve/modify my design, code and any other alternate approaches. please advise
0
Comment
Question by:gudii9
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
  • 2
8 Comments
 
LVL 14

Assisted Solution

by:CPColin
CPColin earned 1000 total points
ID: 41775511
Your line 2 there does nothing. The only way you could really change anything here is by using System.arrayCopy(), which performs essentially the same operation as the loop you've written. Looks like that website doesn't let you do that, though.
0
 
LVL 16

Accepted Solution

by:
krakatoa earned 1000 total points
ID: 41775513
Yes, you could make it a bit shorter . . .

public String[] wordsFront(String[] words, int n) {
  return Arrays.copyOfRange(words,  0,  n);
}

Open in new window

0
 
LVL 16

Expert Comment

by:krakatoa
ID: 41775516
using System.arrayCopy(),

I have said before that Codingbat does not permit System calls ! How could it? Where do you suppose that  output would go ??
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 14

Expert Comment

by:CPColin
ID: 41775522
Well arrayCopy() doesn't do output, so there's no technical reason to disallow that specific method. Interesting that they allowed Arrays, without a package name; they must be importing stuff from java.util behind the scenes.
0
 
LVL 16

Expert Comment

by:krakatoa
ID: 41775541
It's the SYSTEM API call that's wrong!!!
0
 
LVL 14

Expert Comment

by:CPColin
ID: 41775545
I'm not sure what you're on about. I know the site disallows use of members of the System class, seeing as I mentioned that in my first comment.
0
 
LVL 7

Author Comment

by:gudii9
ID: 41775684
package com.solution;



	import java.lang.*;

	public class SystemDemo {

	   public static void main(String[] args) {

	   int arr1[] = { 0, 1, 2, 3, 4, 5 };
	   int arr2[] = { 5, 10, 20, 30, 40, 50 };
	    
	   // copies an array from the specified source array
	   System.arraycopy(arr1, 0, arr2, 0, 2);
	   System.out.print("array2 = ");
	   System.out.print(arr2[0] + " ");
	   System.out.print(arr2[1] + " ");
	   System.out.print(arr2[2] + " ");
	   System.out.print(arr2[3] + " ");
	   System.out.print(arr2[4] + " ");
	   }
	}

Open in new window

array2 = 0 1 20 30 40
something like above instead of 2 give n but coding bat wont allow with System class methods but allows Array class methods which is bit strange
0
 
LVL 7

Author Comment

by:gudii9
ID: 41775688
package com.solution;

import java.lang.*;
import java.util.Arrays;

public class SystemDemo2 {

	public static void main(String[] args) {
		String words[] = { "a", "b", "c", "d" };
		System.out.println("vslue is--->" + Arrays.toString(wordsFront(words, 1)));		

	}

	public static String[] wordsFront(String[] words, int n) {
		String[] newwords = new String[n];
		// copies an array from the specified source array
		System.arraycopy(words, 0, newwords, 0, 1);
		// System.out.print("array2 = ");
		System.out.print(newwords[0] + " ");
		// System.out.print(newwords[1] + " ");
		// System.out.print(arr2[2] + " ");
		// System.out.print(arr2[3] + " ");
		// System.out.print(arr2[4] + " ");
		// return System.arraycopy(words, 0,newwords,0, n);
		return newwords;

	}

}

Open in new window



eclipse it is working fine

a vslue is--->[a]
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
Suggested Courses

610 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question