Code Will Not Calculate Or Print Intersection And Difference Of Two Sets Using Arrays

Nov 19, 2014

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

[code]...

View Replies


ADVERTISEMENT

Program Will Not Calculate Or Print Out Intersection And Difference

Nov 19, 2014

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

The difference of A-B is:

View Replies View Related

Code Of Union And Intersection Of Two Arrays Without Using Collections

Apr 6, 2014

I have searched on internet but did not find something useful. I do not want to use collections. here's what i have written

class ArrayDemo3
{
static void union(int x[], int y[])
{
System.out.println("Union Elements:");
for(int i=0; i<x.length; i++)
{
for(int j=0; j<y.length; j++)

[code]....

View Replies View Related

Intersection And Union Of Two Sets

Apr 6, 2014

class ArrayDemo3
{
static void union(int x[], int y[])
{
System.out.println("Union Elements:");
for(int i=0; i<x.length; i++)

[Code] ....

This is what i have tried. I do not want to use collections.

View Replies View Related

Printing Out Intersection Of Two Arrays

Nov 24, 2014

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

View Replies View Related

What Is The Difference Between Regular Arrays And Multi Dimensional Arrays

Jun 28, 2014

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.

View Replies View Related

How To Calculate Difference Between Two Dates

Oct 19, 2014

I have been tasked with creating an invoice (school assignment). Part of the calculations is creating an interest depending on the amount of days between the current date entered, and invoice date entered (The program prompts the user to enter both the current and invoice dates by asking for the day, month and year).

We are also supposed to set the contructor to a default date of 1/1/1900.. but I don't know how or where to code that.

How to calculate the difference between the CurrentDate and Invoice. I have displayed the dates to the user as follows.

public void displayDate() {
System.out.println("
Today's Date: " + month + "/" + day + "/" + year);
}
public void displayInvDate() {
System.out.println("
Invoice Date: " + invMonth + "/" + invDay + "/" + invYear);

View Replies View Related

How To Calculate Difference Between Two Entered Days Of Week

Mar 19, 2014

I need to create a simple Java program that calculates the difference between two entered days of the week with the following requirements:

-Ask the user to enter in a day of the week by name (e.g. Monday)
- Ask the user to enter in another day of the week by name (e.g. Friday)
- Create a static method other than main that takes in two Strings as input, and returns an integer value equal to the difference between the two days
- Use an array with the days of the week in it to find each entered in day of the week, and use their indices to calculate the difference
- Output the difference for the user to see

View Replies View Related

Gradebook Program - Calculate Averages Of 25 Students Using 2D Arrays

Apr 16, 2014

I am making a gradebook program that calculates the averages of 25 students using 2D arrays in which the students have four test grades. I have figured out how to display the averages of each student in one column but I cant seem to figure out how to display the letter grade of the average into another column.

Right now my program reads from a .txt doc

Heres what I've got.

TestGradeBook.java

import java.util.*;
import java.io.*;
public class TestGradeBook {
public static void main (String [] args ) throws IOException{
//Declarations
final int ROWS = 100;
final int TESTS = 4;

[Code] .....

GradeBook_Data.txt
Name100100100100
// basic name and grades

View Replies View Related

How To Calculate And Display Win / Loss For Two Different Teams Using Boolean Function And Arrays

Jan 28, 2015

I am still new to Java and have an assignment that I am stuck on. I believe I got the boolean function correct, however I cannot figure out what to write in the main function. I have exhausted searching, and trying different loops, arrays, etc.

View Replies View Related

Arrays To Solve And Print A Kakuro Board

Feb 11, 2015

I am working on and am stuck on an assignment where we draw and then solve a Kakuro game.we have been given DrawingPanel.java and DrawGrid.java which should draw the board game grid then we need to pass a .txt document which has the game information

I tried getting my readGrid function to read the input file and store it in arrays but it is not working?

//main -get arguments -make grid -solve it
import java.io.File;
import java.util.Scanner;
import java.util.Arrays;
public class Kakuro {
static File gridFile; // The input file describing the grid

[code]...

View Replies View Related

Calculate If Code Matches

Jan 19, 2015

I have for example a letter code "EECC"...Now i want to have a good loop that checkes if the code typed in by the player matches or not? For example if player types CECE it should say it matches because C occurs 2 times and E occurs 2 times. This function must return than 4.If player types CEEE, it must return 3, because the code has 2 C en 2 E, player input is 1 C and 3 E, the last E is wrong.I've tried some loops with breaks but they all fail..

for example:

String test = "EFCC";
String test3 = "ECEC";
for(int i = 0; i < test.length(); i++){
matchFound = false;

[Code] .....

This returns 2 instead of 3.

View Replies View Related

How To Reduce Number Of Parallel Arrays In Code

Mar 8, 2015

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());

[code]....

View Replies View Related

Getting Error On Java Code Using Parallel Arrays

Dec 1, 2014

This is my code.

public class PA9 {

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)

View Replies View Related

Getting Error With Java Code - Using Parallel Arrays

Dec 1, 2014

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

import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.Scanner;

[code]....

View Replies View Related

Unable To Print Out Results Of Program That Calculate Number Of Seats

Nov 11, 2014

I'm trying to print out the results of a program that calculates the number of seats the parties will get in an election.I have to print the partial results and the national results.

I can print te number of seats per party in each constituency, but how can i sum all seats per party in each constituency and print the national results?I'm working with vectors, which I know it might not be the best option, but everything is working, except the fact that I can't loop throuhg the vector and retrieve the total sum per party.Like, adding the seats for labour party in Constituency A and B and C, etc, and print the sum. And do the same for conservative party.Is it possible, or doIhave to create a method in Parties class to solve the problem?

This is what I have now.

for (Parties p : h.geral) {
show += String.format("Constituency - %5s - %5s - %d%n",
p.getConstituency(), p.getParty(), p.getNum_seats());
}

View Replies View Related

Arrays And Number Generator - Determine And Print Largest And Smallest Values

Dec 4, 2014

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

View Replies View Related

Java Code To Calculate Variance Of A Set Of Number?

May 2, 2014

I'm trying to write a java code that calculates the varince of a set of number and I wanted to know if this is the proper way to do it.

for (int i = 0; i <= (myArray.length - 1); i++){
variance = Math.pow((myArray[i] - average), 2);

}

View Replies View Related

Java Program That Calculate And Print Bill For Cellular Telephone Company

Nov 6, 2014

package p6;

import java.util.Scanner;
public class Bill {
public static void main(String[] args) {
Scanner console = new Scanner(System.in);
//final variables Premium Service
final double monthlyChargeP = 25.00;
final double FordayminutesP = 0.10;

[Code] ....

I can't find the syntax on {} my block is highlighted red in my application

View Replies View Related

Input Length And Width - Calculate And Print Perimeter And Area Of A Rectangle

Feb 16, 2015

Create a program to input the length and width of a rectangle and calculate and print the perimeter and area of the rectangle. To do this you will need to write a Rectangle class and a separate runner class. Your program should include instance variables, constructors, an area method, a perimeter method, a toString method, accessor and mutator methods, and user input. Your runner class should include 3 Rectangle objects. One default rectangle, one coded rectangle, and one user input rectangle. All methods should be tested in the runner class.

This is my code:

import java.util.Scanner;
 public class Rectangle {
double length;
double width;
public Rectangle() {

[Code] ...

What have I done??? I have created this program using the few different resources with which I am supplied, but I don't understand the resources.

View Replies View Related

Netbeans 7.3 - When Adding Print Code Main Method Is Not Recognized

May 13, 2013

I am writing a program in the GUI Builder and found that when I add code to print user input from text fields to a text file, the main method is no longer recognized. It throws all kinds of errors.

I have been able to get the code to work separately in a project file. Also, have been able to print to the console.

View Replies View Related

ArrayList - Intersection Between Lines Of Data

May 24, 2015

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,

line 1 data:5 1 1 1 2 1 3 1 1
line 2 data:5 4 4 5 7 10 3 2 1

try {
File f1 = new File("C:/users/User1/Desktop/Datasets/Dataset.txt");
String filename = f1.getAbsolutePath();
FileReader reader = new FileReader(filename);
BufferedReader br1 = new BufferedReader(reader);

[Code] .....

View Replies View Related

Displaying Sets Of Points

Jan 20, 2014

Here is my code

Java Code: //Points Display
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
import java.awt.Graphics;
import java.awt.Color;
import java.awt.Dimension;
import java.util.Random;

[code]...

1. Why, when I press "New" rapidly, will it skip painting shapes sometimes/adding to totcount.

2. How can I make it so that The box in the background(which is currently connected in the wrong way as I was getting an error when I connected it correctly) can be modifiably large. AKA, I want to draw a grid in the background that can be different sizes, depending on the values of a variable xsize and ysize.

3. What can I do to clean this up? All I want it to do is display the convex hull of the four points given in the int[][] displaypoints.

View Replies View Related

Given Array A With Elements 0 To N-1 - Create N Sets

Jan 27, 2014

You are given an array A with elements 0 to n-1, numbers can be repeated in the array. Create n sets where

S[i]={a[i],a[a[i]],a[a[a[i]]]...}.

Set has all elements unique. Find the size of the largest set.

Input:

First line contains n, size of the array. n<1000

Next lines contains n numbers, each element of the array

Output :

Prints one number: Size of the largest set.

Sample Test Case:

Input: {3,1,2,0}

Output: 2

Explanation:

Four possible sets are

{3,0},{1},{2}{0,3}

View Replies View Related

Using RetainAll To Compare Contents Of Sets

Nov 16, 2014

I am trying to re factor the below code, so that the winning lotto numbers can be checked against more than one lotto line e.g. if the user decided to buy two lotto tickets.

public class Lottery {
public static final int NUMBERS = 6;
public static final int MAX_NUMBER = 40;

public static void main(String[] args) {
// get winning number and ticket sets
Set<Integer> winningNumbers = createWinningNumbers();
Set<Integer> ticket = getTicket();
System.out.println();

[code]....

View Replies View Related

Converting Range Statements To Sets In Java

Mar 20, 2014

I am trying to Extract the ranges of Variables from a Text File. I extracted lines of the forms X in 1..10 Y in 12..50 Z in 0..19 / 66/ 95..100 Where X in 1 ..10 states that X takes values from set 1 to 10 Similarly for Y and for Z its a Union of different ranges of the values (0 to 19, union 66,union 95 to 100)

I want to Map these Variables to their respective sets using Hashmap where Key is Variable name and value will be a Set. My Hashmap Signature is HashMap> hm=new HashMap>();

Java Code:

while((line=br.readLine())!=null) {
Matt=Patt.matcher(line);
if(Matt.find()) {
//System.out.println(line);
String []s=line.split(" ");

[Code] .....

I am stuck at extracting the variables ranges from these plain strings.

View Replies View Related







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