Code Java Method That Accepts ArrayList Of Integers And Integer
Mar 18, 2014
How do I code this without having the need to use iterator? Code a Java method that accepts an ArrayList of integers and an integer. The method should delete all elements in the array list exactly divisible by the integer and return the number of remaining elements.
Write a method named hopscotch that accepts an integer parameter for a number of "hops" and prints a hopscotch board of that many hops. A "hop" is defined as the split into two numbers and then back together again into one.For example, hopscotch(3); should print:
i am interested to add integer objects and String objects into any collection object ..... while iterating the collection object i am not interested to do any type cast in java
So I wrote a method that simply calculates the sum of all integers between 1 and a given integer n. The method works fine however, as n gets big the solution will have time and space problems. Some I'm just curious if there is a better method than my iterative one that would produce a better Big O value.
public static int sum(int n) { int total = 0; for (int i = 1; i <=n; i++) total += i; return total; }
So basically I have to create a java program that accepts a string and does the following:
1.) Insert a blank space before capital letters 2.) Convert any capital letters into lower case except the first letter(if it is capital).
So if the user enters "HiMyNameIsJohn", the result should be "Hi my name is john". I am having problems inside my changeMe method. Everything works fine including putting a blank space in between upper case words. So from the previous example, my code would show "Hi My Name Is John". I am having issues making the code to convert the upper case letters into lower case. Im pretty sure the code should be right after the str.insert(i, " "). But I do not how to code it. Ive thought about Character.toUpperCase(ch); but doesnt work.
Java Code:
import java.util.*; public class WordSeparator { public static void main(String[] args) { String word1, word2; Scanner userInput = new Scanner(System.in);
-Create a main method declares and creates an integer array called nums that can hold 15 integers.
-Use a for loop to fill that array with multiples of 3: 0, 3, 6, 9, etc.
-Then use similar for loop to print each value in the array on one line, with each value separated by a single space.
-Compile and run the program to see the result:0 3 6 9 12 15 18 21 24 27 30 33 36 39 42
As you write other methods, you'll also modify the main method to make calls to them. The printArray MethodWrite a method called printArray that accepts an integer array as a parameter. This method does not return a value, and must be declared as static so that the main method can call it. Instead of printing the array in the main method, move that loop into this method. Call the printArray method from the main method. Compile and run the program to verify it prints the sam result as before.Add a println statement so that after printing the array values on one line, it then moves to the following line.Finally, modify the loop in the printArray method so that, instead of using a traditional for loop, it instead uses a for-each loop. Compile and run the program again.
Part III: More Array Methods
The linearSearch Method In lecture we looked at a method that performed a binary search on a sorted array. A much simpler (though much less efficient) search is a linear search, that simply starts at the front of the array and looks at each element in turn until it finds it or reaches the end.Create a method called linearSearch that accepts an integer array and a single int value as parameters. The goal of the method is to find the second parameter (the target) in the array. The method should return a single int representing the index of the target value. This method should not print any output itself. In this method, use a traditional for loop to scan through the elements in the array. As soon as you find the target value, return the index of that value.
If you scan through the entire array without finding the target value, return a -1.Modify the main method to call the linearSearch method and print the results. Call it twice, searching for the value 18 (which it should find) and the value 10 (which it should not). Including the previous activity, the output of the main method should now look similar to this:0 3 6 9 12 15 18 21 24 27 30 33 36 39 42
The index of 18 is 6
The index of 10 is -1
The sumArray Method
The sumArray method should take an integer array as a parameter and return a single integer representing the sum of all values in that array.Use a for-each loop to access each value in the array and compute a running sum. After the loop, return the total.Call the method from the main method, producing the following augmented output:0 3 6 9 12 15 18 21 24 27 30 33 36 39 42
The index of 18 is 6
The index of 10 is -1
The sum of this array is 315
The addValue Method...The addValue method should accept an integer array and a single int as parameters. The purpose of the method is to add the second parameter to EACH value in the array. The addValue method does not return a value, but the elements inside the array will be modified. Call the addValue method from the main method, adding 100 to each element in the array. Then call the printArray method again to see the modified array values:0 3 6 9 12 15 18 21 24 27 30 33 36 39 42
The index of 18 is 6
The index of 10 is -1
The sum of this array is 315 100 103 106 109 112 115 118 121 124 127 130 133 136 139 142
Test a Different Array..Finally, duplicate the content of the main method to perform similar tests on another array. Instead of filling it with multiples of 3, fill it with multiples of 4. And instead of using an array size of 15, use an array size of 20.Modify the values search for to include one that is in the array and one that isn't.Rerun the main method and carefully check the results.If you haven't been doing it all along (which you should), make sure the appropriate class and method documentation is included.When you're satisfied that all methods are working correctly, modify the main method to delete the second array tests.
Write a function that accepts an array of non-negative integers and returns the second largest integer in the array.
Return -1 if there is no second largest.
The signature of the function is int f(int[ ] a)
Examples:
if the input array isreturn{1, 2, 3, 4}3{{4, 1, 2, 3}}3{1, 1, 2, 2}1{1, 1}-1{1}-1{}-1
In the signature what I understood is, I should write my function with the given signature,
The return type is "int"
method name is "f"
parameter is "a" right ?
Writing my doubts beside the particular line in the code
public static void main() // In the answer why they didn't use the class ?
In main method why they didn't use parameters ?(String[] args)
{ a1(new int[]{1, 2, 3, 4}); // what is "a1" here is it array name ? this line initializing the array ? a1(new int[]{4, 1, 2, 3}); a1(new int[]{1, 1, 2, 2}); a1(new int[]{1, 1}); a1(new int[]{1}); a1(new int[]{}); }
static int a1(int[] a) // what is "a" here parameter ? and "a1" is method name ? why they used the array name and method name same ?
{ int max1 = -1; int max2 = -1; for (int i=0; i<a.length; i++)
How do I code this so that after the user has added to the arraylist 'theFruit' if they then press 'V' to view all fruit it includes the default fruit as well as the fruit they've added?
Also in the method 'AddFruit' it only allows me to add 2 fruit before printing. Why is this?
import java.util.ArrayList; import java.util.Scanner; public class StkOv { public static void main(String[] args) { TheMenu();
I have a problem where I have to create random integers from 1 to n + 1. When the array is displayed I have to find the missing integer from the values that are displayed and show that value. I have to use O(n^2) and O(n) operations. I have created this code so far but I cannot figure out how to make my values display n + 1. It is only displaying values 1, 2, 3, 4 and 5 in random order. I also cannot figure out how to find the missing value. I have created a boolean displayed statement but can't determine how to use it in this code.
=Java import java.util.Random; public class Task6 { public static void main(String[] args) { int[] numbers = new int[7]; //create array of numbers Random random = new Random(); boolean displayed = false; //boolean value to determine if number was displayed
public class MyInteger { private int value; public MyInteger(int number){ value = number; System.out.println("Constructor created with value of " + value);
[code]....
I can't seem to get the value for integer1 and integer2 to pass with the setValue method. I get a runtime error stating the I need to have an int value for these two integers.
Write a method maxOccurrences that accepts a list of integers as a parameter and returns the number of times the most frequently occurring integer (the “mode”) occurs in the list. Solve this problem using a map as auxiliary storage.
This program simply adds integers to an ArrayList and then prompt the user if they would like to delete specific numbers that they don't want.
The output that I get when I want to delete numbers like 2 and 4 from 1 2 3 4 5 is 1 2 3 4 5 instead of 1 3 5.
Java Code:
import java.util.ArrayList; import java.util.Scanner; public class AL { // A regular array like int arr[] has to have its size declare before run-time and it's not dynamic which mean it can not grow or expand on its own. static Scanner input = new Scanner(System.in); // You have to use reference types(Integer, Double, Float); not primitive types(int).
I need to access a method from a dll in java as below and is giving an error
"Exception in thread "main" java.lang.UnsatisfiedLinkError: Testdll.Decrypt(Ljava/lang/StringLjava/lang/String; at Testdll.Decrypt(Native Method) at Testdll.main(Testdll.java:31) " I have included the jna-4.0.0.jar and the HashMatchCryptography.dll to the project in eclipe and the java-library-path has the path for the lib
I am very new to coding and I am struggling with one of my intro to Java Projects. This project involves creating a GUI that has a list of trees and their possible heights, giving each tree a range for their possible minimum and maximum heights.
I am having trouble creating this method for the TreeDatabase (TreeDB class):
public String queryByPossibleHeight(int key) Instructions for this method are as follows: this method creates and returns a single string that contains the trees that can be found such that the value key is in the range for possible height. Traverse the entire ArrayList of Trees and for each one of the trees contained in the ArrayList invoke the method inRange() from the class Tree. Pass to the method inRange() the parameter key. If inRange() returns true, invoke the method toString() from the class Tree. Append the value returned by invoking the method toString() to the String result, adding a “” at the end. Once the entire ArrayList has been traversed, return the String result.
My 3 classes so far (I haven't gotten to the actual GUI yet):
public class Range { private int low; private int high; private int value; public Range(int plow,int phigh){ low = plow; high = phigh;
[Code]...
As you can see in the last TreeDB class, in the public String queryByPossibleHeight(int key) method I have something wrote in, but I was just testing random things.
I'm having an issues with adding integer values to a string list. The question is asking me "the method should iterate over runners, and for each runner generate a random number between 90 and 180 (inclusive) which should be used to set the time (in minutes) for that runner."
I have been able to get the random number and iterating over the runner arraylist but I haven't been able to figure out how to add the values generated into the runners list. I am also using BlueJ.
Here's the whole code I have at the moment:
import java.util.*; import java.io.*; import ou.*; import java.util.Random; /** * Write a description of class MarathonAdmin here. */ public class MarathonAdmin { // instance variables - replace the example below with your own
My program gives me an error when I try to "digUp" the purse. The error appears in the purse class.
import java.io.*; public class Purse { //Fields public double gp; public double gold; public double silver; public double copper; public double platinum; public static final double GOLD_VALUE = 1;
I am trying to code using error handling and I am a bit confused on how to go about doing it correctly. My code converts a string that has all numbers into an integer and the error handling should recognize that if it isn't a proper number and ask the user to try again or enter 'q' to quit.Do I place a throw new exception in the try block and put conditionals like if charAt(i) is some letter or a symbol then throw new exception?
Java Code:
import java.util.Scanner; public class practice { public static void main(String[] args) { Integer finalValue = null; boolean validValue = false; Scanner scan = new Scanner(System.in); int result = 0;
Develop a method that accepts as data input three integer numbers, calculates the sum and the product of the numbers, and displays the sum and product.
Write a program that will read two numbers and an integer code from the keyboard. The value of the integer code should 1, 2, 3, 4. If the value of the code is 1, compute the sum of the two numbers. If the code is 2, compute the difference (first number minus second). If the code is 3, compute the product of the two numbers. If the code is 4, and the second number is zero, compute the quotient (first divided by second). If the code is not equal to 1,2,3,4, display an error message. The program is then to display two numbers, the integer code and the computed result to the screen
here is the code that I have so far:
public static void main(String[] args) { Scanner read = new Scanner (System.in); int num1, num2, code, sum; System.out.println("Please enter a number"); num1 = read.nextInt();
I am trying to create a method that takes an array of integers and prints it out using System.out.print. I'm having trouble creating the right way to print it out since I cannot find a way to convert the int array to a string to print it out.
public static String printArray(int[] num){ for (int i=0; i<num.length;i++){ String msg = num[i]; } return System.out.print(msg + " "); }
OK very similar to switches & cases & ifs, but I'm wondering if I can do something like that:
public Method[] method; method[0] = walk(); method[1] = run(); method[2] = stop(); for (int i = 0; i < x; i++){ if (i == myNumber) { run m[i]; return; } }