Determine Number Of Subsets Of K Items Chosen From A Set Of N Distinct Objects
Sep 22, 2014
Part of my program requires writing a function that determines the number of subsets of k items that can be chosen from a set of n distinct objects ("n choose k" or n!/((n-k)! n!)). The only errors I am getting are from the first line of the function:
// n choose k: distinct subsets of k items chosen from n items
public static int choose (int n, int k) {
if (k <= 0)
return 1;
else
return choose(n--, k--) * (n/k);
}
This is supposed to get a certain number of items, and then calculate how much every item costs. It adds up the sum of those prices and checks if it is over 150$, if it is, it returns true, if it's not, it returns false.
import java.util.*; class Cust { public static boolean DelPay (int a) { Scanner reader=new Scanner (System.in); int b,s=0; for (int i=1;i<=a;i++) { b=reader.nextInt(); s=s+b; } if (s>150) return true; return false; } }
Write a program that detects Fibonacci numbers. Prompt the user to input a positive integer. Upon input, the program will determine if the number is either a Fibonacci number or not. If a Fibonacci number, then the order of the number in the sequence must be output. If not a Fibonacci number, then the Fibonacci numbers above and below it (including their order in the sequence) must be output. Once it finishes, the program will prompt the user for a new number. The program will exit if the user enters a string (such as “quit”) instead of an integer. Use the sample output file, fib-seq-det.txt, to view a sample session
This is my project, I wrote a programs that tells you if the input number is a fibonacci number or not. For some reason it only works for some Fibonacci numbers but not all of them.
import java.util.Scanner; public class While { public static void main(String[] args) { System.out.println("Welcome to the Fibonacci Sequence Detector "); Scanner in = new Scanner(System.in); System.out.print("Please input a number for analysis: "); int input = in.nextInt();
I have created a binary search with 3 subsets, some aslo call it a ternary search and have come up with a minor problem. If you run the code as posted below it just runs until you quit it. If anyother value in the array is searched it is found.
/* public class BinarySearchDemo { public static void main(String[] args) { String[] songs = {"Ace Of Spades", "Beyond the Realms Of Death", "Breaking The Law",
I'm trying to learn Java and my current project is to write a short program to determine the factorial of a number entered by the user. I haven't looked. There may be a method that will do it, but I want to use a for loop specifically.
What I have compiles just fine. I'm actually pretty thrilled just with that. Here is what I have:
class factorial { public static void main( String[] args) { Scanner scan = new Scanner(System.in ); int num; int product = 1;
So basically we have this question to do : Write a method dayNumber that determines the number of days in a year up to and including the current day. The method should have three int parameters: year, month, and day. If the value of any parameter is invalid, the method should print a warning message and return the value zero. The table gives some examples of the action of the method. Accept any non-negative year as being valid. You may want to assume the existence of a method numberOfDays that returns the number of days in a given month of a given year. And you should have a method call isLeapYear, if the user enter a year that is a leap year.
This is what I did so far:
class dayMonthYear { public static void main(String[] args) { System.out.println("Enter a year"); int year = In.getInt(); System.out.println("Enter the month for '1' to be January - '12' to be december");
[Code] ....
It works and compiles but my problem is that: let say I enter "12" for December and December has 31 days, so what my program will do since December has 31 days, it thinks each month has 31 days and add them up which will give me 372 when it's suppose to give me 365. How do I make it that it won't do that and that it will work if the year is a leap year too.
/* Purpose: To write the methods and the rest program. The program should fill a 4 X 4 2 dimensional array with random numbers between 100 and 200. The program should then determine and print the largest and smallest values within the array using two Methods Largest and Smallest. The program should then determine and print the number of values within the array that are even using a function called Even. The program should also enter a loop that will only terminate when the user inputs a -1 as a choice. The loop should continue to ask the user to guess a number that was randomly generated. The program should call the Findit function to determine if the number was in the array. The program should print out the values in the array when the user selects a -1 and then terminate. */
import java.util.Scanner; import java.util.Random; public class LNFI_2DArray { public static void main(String[] args) { int guess; int[] array = new int[4];
[Code] ....
I just had this code working, then all of a sudden i was hit with a 'keyboard leak' error code.
so for class I have an assignment that involves printing distinct numbers. the assignment is as follows: Write a program that reads in ten numbers and displays the number of distinct numbers and the distinct numbers separated by exactly on space. Here is what the input and the output are supposed to look like:
Input: Enter Ten Numbers: 1 2 3 2 1 6 3 4 5 2 Output: The Number of distinct numbers is 6 The distinct numbers are: 1 2 3 6 4 5
So far I have the entire code but right now it only prints out the distinct numbers, not how many distinct numbers there are. what that part of the code would look like?Here is my current code:
public class exer75 { public static void main(String[] args) { java.util.Scanner input = new java.util.Scanner(System.in); //create scanner system System.out.println(""); System.out.print("Enter Ten Numbers: "); //prompt the user to enter 10 numbers int[] values = new int[10]; //create an array to hold the numbers
I am able to get the integer in the array printed but I am lost on how to hold and print the distinct numbers.
This program will read in 10 numbers from the user via the console. It will display on the console only the distinct numbers, i.e. if a number appears multiple times in the input it is displayed only once.
Here is a sample test run: Enter an integer: 2 Enter an integer: 6 Enter an integer: 1 Enter an integer: 8 Enter an integer: 6 Enter an integer: 1 Enter an integer: 2 Enter an integer: 4 Enter an integer: 7 Enter an integer: 5 The number of distinct values is 7 2 6 1 8 4 7 5
I'm working on a standalone GUI under Windows using JAVA SWT and eclipse. I have a colour scheme based on six colours inplemented as:
RGB rgb01=new RGB(233, 150, 122); final Color color01=new Color(Display.getCurrent(),rgb01); RGB rgb02=new RGB(250, 128, 114); final Color color02=new Color(Display.getCurrent(),rgb02); RGB rgb03=new RGB(255, 160, 122); final Color color03=new Color(Display.getCurrent(),rgb03); RGB rgb04=new RGB(255, 165, 0); final Color color04=new Color(Display.getCurrent(),rgb04); RGB rgb05=new RGB(255, 140, 0); final Color color05=new Color(Display.getCurrent(),rgb05); RGB rgb06=new RGB(255, 127, 80); final Color color06=new Color(Display.getCurrent(),rgb06);
I have 8 sets of six colours prepared. I'd like to let the user choose a colour scheme and even change it while they use the application.
As can be seen the Color s are with final as if they are not the code below gives an error.
I need figuring this problem out. It appears that I am attempting to generate a permutation of the string "ABCDEF" 720,000 times using this method:
In the second method, j is chosen randomly in the range from 0 to i (inclusive).
Once the permutations are generated, the program will proceed in counting the number of times each permutation occurs, calculating the chi square statistic of the situation, and creating the chi square distribution with 719 degrees of freedom, then outputting the statistic and the chi square probability of the permutations. The generatePermutation method is where all the magic happens. Only trouble, I can't figure out what I equals to. The times where I think I have i as a correct value only give me the program outputted as 1.0 probability every time. What it needs to be doing is outputting variable probability as a number always between 0 and 1, not 1 all the time. Here is my code:
I am taking the Class Algorithms and Datastructures and got an assignment for Lab that really throws me off. The goal is to create an Array out of a given CSV file, implement several Methods that get the size of array, etc.
I am still stuck in the first part where the CSV has to be imported into the Array. My problem is that I need a mechanism that figures out the needed size for the Array, creates the array, and only then transfers the data from the CSV.
The list consists of the following wifi related values:
MAC-Adress, SSID, Timestamp, Signalstrength.
These are on the list, separated by comma. The Columns are each of these, and the rows are the four types of values making up the information on a certain wifi network.
The catch is, we are not allowed to use any of the following:
java.util.ArrayList java.util.Arrays and any class out of java.util.Collection.
So far I used the BufferedReader to read in the file and tried to implement the array, but I get an arrayindexoutofboundsexception.
Below is my Code (Its still an active construction zone):
public class WhatsThere { public WhatsThere(String wifiscan) throws IOException { } public static void main(String[] args) throws IOException { // WhatsThere Liste = new WhatsThere(String wifiscan); String[][] arrayListe = new String[0][0];
I have an assignment for college that involves placing randomly chosen words from a text file into a 2-d array. I have nearly completed ithowever I am having difficulty with a list of string type.What I have done so far is below,
when I try to run an instance of the program(using BlueJ) I get an error saying there is a null pointer exception at line 35 which is wordsForGrid. add (puzzleWords.get(pos));
I am thinking that this is related to the fact that at this line in the loadWordsFromFile method List <String>words=new ArrayList<String>(); I could not create an empty list of string type, i.e List<String>words = new List<String>();
Is this correct? Or am I looking in the wrong place. The textfile I am using contains over 6000 words on a separate line for each if that makes any difference.
I am trying to find a concise way to write the sort methods for my class. I am supposed to make a program that can sort objects by a number of fields: year, rank, artist and title.
I used an idea from this thread : java - Sorting a collection of objects - Stack Overflow
And I am trying to use the custom comparator for my sort methods. However for some reason, the sortingBy variable fails to recognize any of the enum types.
Whenever I try to set the sortingBy variable equal to one of them, for example:
Java Code:
private Order sortingBy = Year; mh_sh_highlight_all('java');
I get a "Year cannot be resolved to a variable" error.
What I want to be able to do is make it so every time a specific method is called, say, for example sortTitle(), sortingBy will change to Title, then the SongComparator will sort using the case Title.
Is it possible to do this? I can't figure out how to modify SongComparator's object variables that way.
Java Code:
import java.util.Comparator; public class SongComparator implements Comparator<Song> { public enum Order {Year, Rank, Artist, Title} public Order sortingBy;
Which is the best way to keep track of the number of the objects I've created?Is is a good practice to have a static variable, which will be incremented everytime I call a contructor?
I have application written in rest with Jersey-Jackson for JSON processing. All the resources produce and consume JSON. Now, the problem is, it is a server intensive application and large number of request will be hitting the server with large JSON request object. Now, because of this reason, when the JSON object gets converted in to Java object with String fields in it mapping to JSON request values large number of string objects are getting created which is resulting in frequent GC.
I have a problem with my application. It supposed to store 4 different Room objects but when I entered one only it stores tat object variables into all my Array elements. I just need it to store any number of objects as long as it is less than 4.
Java Code:
import java.util.Arrays; import java.util.Scanner; import javax.swing.JOptionPane; class TestRoom { public static void main(String [] args) { String[] roomsInHouse = new String[4];
I have to make a program that prompts the user to enter 10 numbers and at the end it prints out the distinct numbers and then the other numbers that weren't repeated...
I have the part where it prints out the distinct numbers but I stuck on how to make it print out the other numbers that didn't repeat...
import javax.swing.JOptionPane; public class DistinctNumbers { public static void main(String[] args) { String getInput; int input; int[] numbers = new int[10];
I am currently working on a project where I need to return data from a database over RMI to a client who requests it. Some of the fields in the Data Object can not be seen by the client so I need to create another object to send over the network instead. The method I use is this...
public static SerializableObject createSerializableObjectFromDataObject(DataObject dataObject){ SerializableObject serializableObject = new SerializableObject(); serializableObject.setField(dataObject.getField()); serializableObject.setAnotherField(dataObject.getAnotherField()); return serializableObject; }
Is there a better way of doing this? I am creating many subclasses DataObject which all require this static method to be implemented and I can't push it into the superclass because each one needs custom behaviour.
I try to create a jsf project within ejb which is add new car with entering attributes listing attributes and search by make.
Add and list methods working well , but have problem of list method. I tryed many combinations (using enhanced loop, iterative loop) but i cant provide working well. Always outputText returns nothing ,when i enter attributes.
Here is ejb code for adding ,getting and listing car items :
@Stateful public class CarsBusiness implements CarsBusinessLocal { List<Car> cars; public CarsBusiness() { cars = new ArrayList<Car>();
I facing issue with nested <c:forEach in my jsp page.I am using jstl.jar..Here is my code
in JAVA I have --> List<ProductDefViewBean> productList = new ArrayList<ProductDefViewBean>(); productList.add(objProductDefViewBean); request.setAttribute("ProductList", productList); <%@ taglib uri="http://java.sun.com/jstl/core" prefix="c" %>
[code]...
javax.servlet.jsp.JspTagException: Don't know how to iterate over supplied "items" in <forEach>
I made my items class and I am storing my item as a String array to list them all and what I want to be able to do is type pickup, have the game read my location and display the items that are avaible to pickup at current location then type the item and store it in my playerInventory. Then, have it check the slot if it is doesn't equal null go to next.
Use a boolean value of 0 = false and 1 = true. then, have it check if the inv array = 1 or 0. if 1 go to next inv. If all are full then, ask player if they would like to replace an item. Use a 2d array for storage for slots and true or false value.
Here is my code
Player.java package com.PenguinGaming; import java.util.Random; import java.util.Scanner; public class Player { public void User(){ Commands commands = new Commands(); Map map = new Map();
I have the following code. how can items be added to that list?
private List<IComponentNode> comp; interface IAAAView { // returns component with given code or null IComponentNode findComp(String a); // returns component with given renderer or null
I tried implementing the bubble sort algorithm into my code, but now my output for the second array (in my code) is giving me a ton of zeros. How I can fix it so the zeros are removed and the only thing that remains in the output for my second array are the fixed numerically?
The first array is the original list of items, and the second array is a copy of the first array, but numerically adjusted.
public static void main(String[] args) { System.out.println("Input up to '10' numbers for current array: "); int[] array1 = new int[10]; int i; Scanner scan = new Scanner(System.in);