Accept List Of Integers As Parameter And Return Number Of Times Most Frequently Occurring Integer

Nov 20, 2014

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.

If the list is empty, return 0.

View Replies


ADVERTISEMENT

Accept Array Of Non-negative Integers And Return Second Largest Integer

May 22, 2015

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++)

[Code] .....

View Replies View Related

Simple Recursion Method - Accept Integer Parameter N And Return Sum Of First N Reciprocals

Nov 1, 2014

I am practicing some basic recursion and I was trying to solve this problem

Write a method sumTo that accepts an integer parameter n and returns the sum of the first n reciprocals. In other words:

sumTo(n) returns: 1 + 1/2 + 1/3 + 1/4 + ... + 1/n

For example, the call of sumTo(2) should return 1.5. The method should return 0.0 if passed the value 0 and should throw an IllegalArgumentException if passed a value less than 0.

This is my attempt to do it , however my output is always a 0.0 , and i do not understand why :

public static double sumTo(int n ){
if(n<0){
throw new IllegalArgumentException();
}
else if (n==0){
return 0.0;

[Code] .....

View Replies View Related

Count And Return Number Of Times Integer Occurs In Array

Nov 16, 2014

I have problems getting the right number of times for each number of the array. Here is my code:

public static void main(String[] args) {
int[] a = { 3, 13, 5, 9, 13, 6, 9, 13, 2, 3 };
int num = 3;
int count = numbers(a, num);
for (int i = 0; i < a.length; i++) {

[Code]...

View Replies View Related

Accept String And Number Of Repetitions As Parameters And Print String Given Number Of Times

Oct 3, 2014

I'm having a hard time with this problem, this is what I have, but I can not use two integers, I have to use one integer and a string...

This is the question:

Write a method called printStrings that accepts a String and a number of repetitions as parameters and prints that String the given number of times. For example, the call:

printStrings("abc", 5);

will print the following output: abcabcabcabcabc

This is what I attempted:

public class printStringsproject {
public static void printStrings(int abc, int number) {
for (int i = 1; i <= number; i++) {
System.out.print("abc");
}
}
public static void main(String[] args) {
printStrings(1, 5);
}
}

View Replies View Related

Method That Accepts Integer Parameter For Number Of Hops

Apr 9, 2014

The instructions:

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:

1
2 3
4
5 6
7
8 9
10

how to start this program.

View Replies View Related

CountDigitx - Take Two Integers As Input And Return How Many Time X Occurs In Number N

Apr 29, 2014

Write a recursive Java method countDigitx that takes two integer numbers as input (a number n and digit x) and returns the number of times digits x occurs in number n:

For example if n=23242 and x=2 then the method should return 3 that is the number of times the number 2 found in n.

View Replies View Related

User Enter 5 Digit Number And In Return List Each Number On Their Respective Lines

Sep 16, 2014

So I am currently writing my first assignment and have run into problems with my coding. The task was to have someone enter a 5 digit number and in return, I list each number on their respective lines. We also must create an error if a number other than 5 digits was entered. My problem is that when they enter a 1 or 2,3,4,6,7,8 digit number.. the error message occurs along with the rest of the messages (listing the numbers, etc). I want the program to end (or even re-ask to enter the numbers) if they incorrectly enter the data.

View Replies View Related

Return Integer That Is The Number Of Strings That Starts With S

Apr 29, 2014

Write a Java code of the method startsWithCount that takes an array of strings words and a String S. The method should return an integer that is the number of strings that starts with S.

For example if:words = { "All", "Arab", "size", "Almond", "Allowed", "here"} and S= "All", then the method should return 2

PHP Code:

public class StringwithCount {
public static void main (String[]args) {
String strings[] = { "All", "Arab", "size", "Almond", "Allowed", "here"};
String output= "";
for ( int i  =  0;  i < words.length; i++) { 
            if (words[i].startsWith("s")) 
                c + +; 
        } 

View Replies View Related

Method Must Return Int Type - If Given Integer Is Strong Return A / If Not Return B

Sep 7, 2014

I want to use a method, which takes for example an int and also returns an integer. For example, if the the given integer is strong return a, if it is notstrong return b. How would you write that in a Code?

I want to use that in a more general way. I want to give a method mlong the value X of the type date and let it return an int. Type date consists of 3 int, one of them is the int month.

mlong should return an int depending on the X.moth. at the moment my code looks like this:

// File1:
public class date {
public int day;
public int month;
public int year;
}

// File 2:
public class monthlength {
public int mlong(date X) {
int t;
t = X.month;
if (t == 1 || t == 3 || t == 5 || t == 7 || t == 8 || t == 10 || t == 12)
{ return 31; }
if(t == 4 || t == 6 || t == 9 || t == 11)
{return 30;}
}
}

View Replies View Related

Method That Accept Object As A Parameter

Jan 3, 2015

I have a method that accepts as a parameter an object:

public void addClient(Client c){ }

Client is a class which has a constructor that has two String parameters:

public Cliente (String name, String lastname){
this.name = name;
this.lastname = lastname;
}

In the main add a Client in this way:

addClient(new Cliente("first", "second"));

Now, i have an array of Client, so I would like to enter within this. Example:

public void addClient(Client c){
for (int i = 0; i<client.length ; i++) { // client is an array of Client object
client[i] = c; // Enter a c in the array, but does not work!
System.out.println("test "+clienti[i]); // print Client@15db9743
}
}

I have used the println for check if worked insertion, but the result shows no

View Replies View Related

Test Driver Does Not Accept String Parameter

Jan 6, 2015

I wrote a couple classes and am trying a test driver but it is having an error I do not know how to solve.

Student Class:

public class Student{
private Course[] courseList;
private static int numCourses;
private final int maxCourses;
public Student(int max){
maxCourses = max;

[Code] .....

Error:
javac tester.java
tester.java:6: error: cannot find symbol
one = new Course(name);
^
symbol: variable name
location: class tester
1 error

Same issue, just only one error as there is only one line. It seems like it does not accept my parameters as it cannot find symbol.

I forgot to put the "" in the brackets, it's been a month since I have looked at any java and made this simple mistake.

View Replies View Related

JTextField That Accept Only Integer Input?

Jul 5, 2014

I have a GUI with a JTextField. I need to check that the input entered by the user is made up of only integers.

For example I accept: 1, 10, 530, ...

I do not accept: -10, 1.5, -6 ...

I would like to implement this control by using a regular expression so I would have a code like that.

JTextField text = new JTextField();
String input = text.getText();
boolean bool = checkInteger(input);
if(bool)
System.out.println("Ok");
else
System.out.println("Error");

The checkInteger(String input) method should check if the input string is correct or not.

How can I do to implement this control with regular expressions?

Is there an easier way to do it?

View Replies View Related

Accept Integer As Input - Display Even Or Odd

Apr 14, 2014

This program is supposed to accept an integer as an input and display the message that the number is even or odd. The main method calls a Boolean method. Write a method private static boolean iseven(int number and the message is printed from the Main method. This is what I have.

import java.util.Scanner;
public class OddorEven {
int number;
public static void main(String[] args) {
Scanner input = new Scanner(System.in);

[Code] ....

I know the message is not being printed from the Main method. I'm not sure how to do that.

View Replies View Related

Program Is To Accept Number And Display New Number After Removing All Zeros

Dec 29, 2014

ex
Sample input:2340980
Sample Output:23498

this is the program i have tried but is dosent work

import java.util.*;
class zero
{
public static void main(String args[])
{

[Code]....

View Replies View Related

Program That Accept Input Of Positive Integer And Sum All Digits

Jan 28, 2015

I am attempting to write a program that accepts input of a positive integer, reports each digit and then the sum of all digits. This is what I have so far:

*/
 package numbersum;
import java.util.*; 
public class Week4NumberSum {
static Scanner console = new Scanner(System.in); 
/**
* @param args the command line arguments

[Code]...

It works if I enter 1234567891 (10 digits)
Enter a positive integer: 1234567891
digit: 1
digit: 2
digit: 3
digit: 4
digit: 5
digit: 6
digit: 7
digit: 8
digit: 9
digit: 1

The sum of the digits is 46

But if I enter 11 digits, it fails:

Enter a positive integer: 123456789123 (12)
Exception in thread "main" java.util.InputMismatchException: For input string: "123456789123"
at java.util.Scanner.nextInt(Scanner.java:2123)
at java.util.Scanner.nextInt(Scanner.java:2076)
at week4numbersum.Week4NumberSum.main(Week4NumberSum. java:26)

java - Scanner error with nextInt() - Stack Overflow gives me some infor for InputMismatchException - which I find described as In order to deal with this exception you must verify that the input data of your application meet its specification. When this error is thrown, the format of the input data is incorrect and thus, you must fix it, in order for your application to proceed its execution.

I don't understand why the 10 digit integer is OK but the 11 or > digit integer is a mismatch.

View Replies View Related

Program To Accept String Float And Integer As Command Line Arguments

Feb 8, 2015

Write a java program to accept a string, float and an integer as command line arguments. String value should be displayed. Also the program should display the maximum value of two numbers. Use Integer.parseInt() and Float.parseFloat()

View Replies View Related

Can Pass In Return Value In Parameter?

Jun 25, 2014

Can I pass in a return value in a parameter? I'm trying to do something like this:

Object o = new Object(anotherObject.method());

Where method returns an int. But when I try doing this it calls that method, rather than passing in the return value..

View Replies View Related

Calculate Sum Of All Integers Between 1 And Given Integer N

Aug 3, 2014

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;
}

View Replies View Related

Declare And Creates Integer Array That Can Hold 15 Integers

Nov 3, 2014

The main Method

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

View Replies View Related

If Given Integer Is Even Return 1 / If Not Even Return 0

Sep 7, 2014

I want to use a method, which takes for example an int and also returns an integer. For example, if the the given integer is even return 1, if it is not even return 0. How would you write that in a Code?

I want to use that in a more general way. I want to give a method mlong the value X of the type date and let it return an int. Type date consists of 3 int, one of them is the int month.

mlong should return an int depending on the X.moth.

At the moment my code looks like this:

// File1:

public class date {
public int day;
public int month;
public int year;
}

// File 2:

public class monthlength {
public int mlong(date X) {
int t;
t = X.month;
if (t == 1 || t == 3 || t == 5 || t == 7 || t == 8 || t == 10 || t == 12)
{ return 31; }
if(t == 4 || t == 6 || t == 9 || t == 11)
{return 30;}
}
}

View Replies View Related

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.

View Replies View Related

Displaying Random Value Of Integers And Then Finding Missing Integer From Values In Array

Feb 7, 2015

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

[code].... 

View Replies View Related

Quicksort With Single Linked List / 1 Node Parameter

Nov 12, 2014

I have a custom linkedList(single) class that uses the provided node class. Now I have another class to QuickSort this.(left out for brevity, i just wanna focus on editing the L.head). However, instead of passing the quicksort method the entire linkedList, I want to pass it just the head from the linkedlist.

My problem is accessing this head node and changing it from the quckSort method/class, and I dont want to delete it or simply just change the element value

Main:

public class TestLinkedList {
public static <E extends Comparable<E>> void main(String[] args) {
MyLinkedList<Integer> L = new MyLinkedList<Integer>();
L.add(3);
L.add(1);
L.add(2);
System.out.println("Initial=" + L);
MySort.quickSort(L.head);
System.out.println("After ="+L);
}
}

QuickSort:

public class MySort {
public static <E extends Comparable<E>> void quickSort(MyNode<E> list) {
list = list.next;
}

Node Class:

public class MyNode<E extends Comparable<E>> {
E element;
MyNode<E> next;
public MyNode(E item) {
element = item;
next = null;

[code]....

View Replies View Related

How To Print A Character Random Number Of Times

Oct 26, 2011

I'm trying to write a method that will print a string of a random number of X's (from 5 to 20) on a line, then another number of X's on the next line, etc. until the random number = 16.

I have to use while loops and probably fencepost algorithms. I'm confused with how to print the number of x's the random number of times over and over.

Right now, I'm pretty sure my for loop is wrong or unnecessary.

So far, this is my code:

public static void randomX() {
int x = 0;
while (x != 16) {
x = (int)(Math.random()*20) + 5;
for (int i = 0; i <= x; i++);
{
System.out.print("x");
}
}
}

View Replies View Related

How To Find How Many Times A Number Appears In Array

Dec 7, 2014

This is my code up to now and I can't do anything to make it work. I want it to tell me how many times the number 3 appears, and the last position it was in. I am getting errors like"Cuanto.java:88: getPosition(double[],double) in Cuanto cannot be applied to (double) ....

a = getPosition(a);" and unreachable statements, and value not found. It's all over the place every time I make a change to it.

Java Code:

public class Cuanto {
static int getPosition(int count,double listOfValues[],
double targetValue) {
int a = 0, i;
for (i=0; i < listOfValues.length; i++) {
if (listOfValues[i] == targetValue) {
a++;

[Code] ....

View Replies View Related







Copyrights 2005-15 www.BigResource.com, All rights reserved