As part of the instructions we are required to use loops and methods to solve to program this code. We are not allowed to use any set functions that are already built in java.The methods for intersection and difference MUST return int value.
import java.util.Scanner; public class setPractice { public static Scanner kbd; public static final int MAXSIZE = 20; public static void main(String[] args) { kbd = new Scanner(System.in);
We had to use these methods with the given parameters and code them correctly. We cannot use any built in java methods for sets.
import java.util.Scanner; public class setPractice { public static Scanner kbd; public static final int MAXSIZE = 20; public static void main(String[] args) { kbd = new Scanner(System.in); int[] setA = new int[MAXSIZE]; int[] setB = new int[MAXSIZE]; int[] intersect = new int[MAXSIZE];
[Code] ....
This is the input given to the program:
How many numbers will be in the 1st set: 3 Enter list of integers for 1st set: 12 3 2
The ascending order for 1st is: 2 3 12
How many numbers will be in the 2nd set: 4
Enter list of integers for 2nd set: 2 3 6 1
The ascending order for the 2nd set is: 1 2 3 6
The intersection of the two sets is: 0 0 0 //Program not correctly print intersection of two sets
The difference of A-B is: //Program is not printing the difference either...
I am trying to create a java program to sort an array in ascending and descending order. Here is the program I created :
import java.util.Arrays; import java.util.Collections; public class ArraySort { public static void main(String [] args) { int [] num = {5,9,1,65,7,8,9}; Arrays.sort(num);
[Code]...
BUT I GET THE FOLLOWING EROOR ON COMPILATION
ArraySort.java:12: error: no suitable method found for reverseOrder(int[]) Arrays.sort(num,Collections.reverseOrder(num)); ^ method Collections.<T#1>reverseOrder(Comparator<T#1>) is not applicable
My friends and me are trying to make online Test taking system. We got a very basic doubt.
We have developed classes and relationship between classes for example : Online Test Taking system will have admin and student class. And Admin will have list of students.. etc.
But question troubled me was: if we use database to store all data for example student details then I can perform all sorts of operations writing sql query and store result in some other database then what is the need of "ArrayList<Student> field in Admin".??
Question is: We can put everything in database and manipulate using database(sql) functions to manipulate it.Then what is the need of Arraylist of anything which is just used to store object details for example student details....??
I'm building a text based "game" where you are communicating with this android creature called Gargoid , at a VERY primitive level . how it works is, simply the user type in a sentence which is decoded for meaning by comparing it with a built in list of words in order to figure out what the user is saying, and then reply with a a relevant response also from a list of built in words. the whole thing would look something like this,
user: what is your name Gargoid : my name is Gargoid, nice to meet you user: how is the weather Gargoid: the weather is wonderful
so far I have 11 arrays which are the following
String[] for user typed in words used for comparison to find meaning ..An Array of String[] , 7 so far, to hold what I call the Gargoid dictionary for example String[] greeting={hi,hello,aloha}, words that indicates greeting int[] called frequency to determine which of the 7 arrays have the greatest "relevancy" to what is being said. and finally another String[] for responses here's the actual code, I want you guys to tell me if there's a way to reduce all this never ending number of arrays? and also is this code a good application of object oriented programming?
MainClass public class GargoidMain { public static void main(String[] args) { TheKeyBoard keyboard=new TheKeyBoard(); TheTranslator translator=new TheTranslator(); TheBrain brain=new TheBrain(); translator.translate(keyboard.userSaysWhat()); brain.respond(translator.userSays());
public static void main(String[] args) throws FileNotFoundException { // create data file to read from File inf = new File("cityPopulationData.txt"); //Create a file to write out to. PrintWriter fileOut = new PrintWriter("output.txt");
[Code] .....
This is my error
Exception in thread "main" java.util.InputMismatchException at java.util.Scanner.throwFor(Scanner.java:909) at java.util.Scanner.next(Scanner.java:1530) at java.util.Scanner.nextInt(Scanner.java:2160) at java.util.Scanner.nextInt(Scanner.java:2119) at PA9.getData(PA9.java:35) at PA9.main(PA9.java:22)
Write a Java program to read from the data file, find the city with the highest population based on the 2010 census data, the city with the highest population growth from 2010 to 2013, the city with the lowest population growth from 2010 to 2013, and the city with the highest density (number of persons per sq. mile).
How do I take the union between an object called on and the object passed in the parameter?I have an interface:
Java Code:
import java.util.Set; /** * An extended Set where additional functionality is added */ public interface NewSet<T> extends Set<T> {
[code]...
Now, the union method requires a NewSet object to be called on. The Union will be performed between the object called on and the object passed in the parameter, which is set.How do I call on a NewSet object in order to take the union between the object called on and the object passed in?Do I just do:
Java Code:
public NewSet<T> union(NewSet<T> set) { NewSet<T> calledOn = new ArrayList<T>(); // ... } mh_sh_highlight_all('java');
My code is like follows. I want add the lines of data from text file to arraylist.After add the lines to array list,i want to get the interesection between the lines of data. But I could not get the intersection and it returns empty. My sample data from specific line in text file as follows,
I have to use methods and loops to code this program. Cannot use any java set or any built in java shortcuts!
import java.util.Scanner; public class setPractice { public static Scanner kbd; public static final int MAXSIZE = 20; public static void main(String[] args) { kbd = new Scanner(System.in);
[Code] .....
This is the input I put in my code and this is the output:
How many numbers will be in the 1st set: 3
Enter list of integers for 1st set: 34 2 56
The ascending order for 1st is: 2 34 56
How many numbers will be in the 2nd set: 4
Enter list of integers for 2nd set: 56 2 33 6
The ascending order for the 2nd set is: 2 6 33 56
The intersection of the two sets is: 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
I am new in Collection, as per my knowledge I have already override the hashCode() and Equals() method in Data class, but when I trying to search the element it is giving "not found". find the bug.
import java.util.*; public class WordCounter { public static void main(String args[]) { HashSet<Data> set=new HashSet<Data>(); set.add(new Data("this",2));
I am just not sure of some theory in collections, ArrayList and LinkedList maximum capacity depends on the memory allocated to the JVM. But according to few people those list has a maximum capacity of Integer.MAX_VALUE.
According to my experiment, those list has a maximum capacity of Integer.MAX_VALUE since the get method of List accept a parameter of int primitive type (index of element), therefore we can conclude that the maximum capacity of List is equal to Integer.MAX_VALUE.
But what about the Map? get() method of map accepts object(the key of the map). So does it mean that the maximum capacity of Map depends on the memory allocated to our JVM? Or its maximum size is Integer.MAX_VALUE also just like Lists and Arrays? Is there a way to prove it? Is Map designed to hold infinite number of data (disregarding the heap memory space exception)?
And also about Stack, Deque? is it also the same as Map (in terms of maximum capacity)?
Today I installed jdk1.8.0_20 on my computer and typed a simple example code in MyEclipse 6.0 IDE, the code listed below:
import java.util.List; public class Test5 { public static void main(String[] args) { List<String> features = Arrays.asList("Lambdas", "Default Method", "Stream API", "Date and Time API"); features.forEach(n -> System.out.println(n)); } }
the MyEclipse Editor shows some errors like "Arrays can not be resolved" and "n can not be resolved".
I did use jdk 8 in the build path, but it seems like the jdk 8 did not function properly.
The Iteratior provides the functionality of traversing and removal which we can achieve through normal for loop and remove() of the data structure.Then, why do we need Iterator explicitly and as an inner class?
I am using this image as a reference to learn the Collection interface better and it seems I must be getting confused with the 'ordered/unordered' types. I thought ordered would mean ordering the objects automatically like a TreeSet using the comparable interface so it could be numerically or alphabetically ordered etc..
Java Code:
public static void main(String[] args) { Set hashSet = new HashSet(); hashSet.add(1); hashSet.add(3); hashSet.add(2); for (Object o : hashSet){ System.out.print(o + " "); // Prints: 1,2,3 (Unordered but puts the numbers in order....)
I imported the collections utility and everything. Tell me if you need to see more of the code. The exact error is binsearch is undefined for the type Collections.
I have a method that draws a polygon on the screen:
public void poly(List<Point> points) { //code }
For the usage of the method, it makes no difference whether points is an array or a List. In fact, switching doesn't even change any of the code in the method since I use a for-each loop. So, my question is, is it a better practice to use a List as an argument, or an array when the method itself doesn't care about which to use?
I mainly would like to know is a int[4][4] or a int [4*4] is more efficient to use. Which takes more storage? Which requires more to process? that kind of stuff.
I'm doubted regarding the implementation of Collections.binarySearch() method on an ArrayList of objects of a custom class Movie.
Here is the movie class :
public class Movie implements Comparable<Movie> { String movieName; String rating; String director; String theme;
[Code] .....
The sort/binarySearch in searchByMovieName function is done with natural sorting (and Comparable Interface in Movie class). I mean no comparators involved here. And the comparator that I used for sorting/binarySearching on Movies Director attribute in searchByMovieDirector function is :
public class MovieDirectorComparator implements Comparator<Movie> { public int compare(Movie movie1, Movie movie2) { return movie1.getDirector().compareToIgnoreCase(movie2.getDirector()); } }
But I was not able to implement binarySearch ?? How to implement the binarySearch here. I have google to see only binarySearch working on Arrays or probably ArrayList of String only, but not on ArrayList of custom objects.
I was a bit confused of the code generated by the Axis2 Code Gen.
I have created a logIn(String username, String password) method in my service and used Axis2 Code Gen to generate the Stub.
I try to access the logIn method from my Client using the Stub like below:
TestServiceStub stub = new TestServiceStub("http://localhost:8080/axis2/services/TestService"); String test = stub.logIn("user","pass").
but instead of UserName and password as the parameters, the Stub created a Login object as the parameter for the logIn method. like the one below:
stub.logIn(Login login1);
I wanted to try the logIn method by providing a static userName and password but it seems impossible because the Stub changed the parameter for the logIn method.
The code here I have works fine if I just want to ask the user to enter four digits: //java application that asks user to input binary numbers(1 or 0) and convert them to decimal numbers import java.util.Scanner; //program uses class scanner public class binarynumber{
//main method that executes the java application public static void main(String args[]){ //declares variables
int digit; int base=2; int degree; double decimal; int binary_zero=0; int binary_one=1; //create scanner for object input
[code]....
The thing is, I want the java application to input more than four digits for the user and I want it to loop it manytimes f until the user ask it to stop.