I have been struggling to find out what to change to allow my code to find the GCF of two numbers. The instance variable in the class is num, so I need to find the GCF of a and num. It works for the example problem of 25 and 15, returning 5, however it will not work for other problems.
public int findGCD(int a)
{
int result = 0;
int newNum = num;
if ((a > num) && (a % num != 0))
My interest in Java leads me to try and print numbers from 1-100. The output should show all numbers from 1-100 and each number that is divisible by by 13 should be replaced with a string "Fuzzy".
public class Fuzzy { public static void main (String[]args){ for(int i = 0; i < 100; i++) if(i % 13==0) System.out.print(i + "fuzzy"); } }
I tried to create file and write the output of my program in it in java when i use WriteLong then the file does not contain long value, how I create this file my program is to print prime numbers between 500000 to 10000000
public class primenumber { public static void main(String[] args) { long start = 5000000; long end = 10000000; System.out.println("List of prime numbers between " + start + " and " + end); for (long i = start; i <= end; i++) { if (isPrime(i)) { System.out.println(i);
I've just written a program that generates 100 numbers that range from 0 ~ 25 using arrays, the program calls another method that sorts the even numbers into a separate array and returns the array. I need it to display both arrays, however, when I run my program, the numbers of both arrays are mixed together, and I'm not sure how to separate them.
[ public class Array1 { public static void main(String[] args) { int array [ ] = new int[100]; for (int i = 0; i < array.length; i++) { array[i] = (int) (Math.random() * 26);
I'm trying to make a program that generates 20 random integers between 1 and 20 and then prints the list of random numbers to the screen. After that, I want to print a different list to screen with the same numbers from the first list only skipping any number that has been already printed to the screen. So two lists are printed to the screen. The first one has 20 random numbers. The second one has those same 20 numbers but only prints the numbers in the first list that aren't duplicated. So if m
y list of 20 random integers contains three 2s and two 14s, only one 14 and one 2 is printed to the second list. Currently, my code generates 20 numbers from 1 to 20 and stores those numbers in an array but I don't know how to print solve the second part of my problem. I don't know how to print the s different list only without duplicate numbers. As a result, my output is nothing because it doesn't print any number from the first list as oppose to skipping only duplicate one.
public void randomNum(){ System.out.println("Twenty random integers: "); int max = 20; // max value for range int min = 1; // min value for range Random rand = new Random(); int[] all = new int[20];
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];
Create an integer array with 10 numbers, initialize the array to make sure there are both positive and negative integers. Write a program to generate two arrays out of the original array, one array with all positive numbers and another one with all negative numbers. Print out the number of elements and the detailed elements in each array.
public class problem3 { public static void main(String[]args){ int[] numbers = {1, 2, 3, 4, 5, -1, -2, -3, -4, -5}; for (int i = 0; i<numbers.length;){ if(i>0){ System.out.println(numbers); } else System.out.println(numbers); } } }
I have assigned random numbers to an 2D array, and I had to write a method that prompts the user for a number between 100 and 200. If that number is in the array, it should return 1. If not, 0. It only stays 0 though, even when the number matches one of the numbers in the array.
public static int Findit(int[][] numbers, int guess) { int Findit = numbers[0][0]; int i = 0; int x = 0; boolean found = false; for(i=0;i<numbers.length;i++)
My current problem is when im trying to choose case 2 and call markItemOnLoan i cant seem to find the title i want after i enter a few. I will include all of my code but i have mentioned what I am currently stuck on :
import java.util.Scanner; public class Library { public static void main(String[] args) { Scanner input = new Scanner(System.in); MediaItem mCall = new MediaItem(); Library call = new Library();// makes instance
How to declare a 2 dimensional array of interger of size 10 by 10. and then set every element of the array to 3. After this I need to add up all the element in this array and return the value.
final int ROWS = 10; final int COLS = 10; int[][] foo = new int[ROWS][COLS]; for (int i = 0; i < ROWS; i++){ for(int j = 0; i< COLS; i++){ foo[i][j] = 3; } }
This is what i have done so far(not 100% if is correct..) but how to add up all the array.
I have an assessment for college, the part of my code that im struggling with is the part where the user enter their first name followed by a space and then their second name. I m using ---------------fullname = JOptionPane.showInputDialog("enter your firstname followed by a space then your second name " ); ------------- to capture this. the issue is if the user only enter 1 name I need to output an error. the issue is I cant find a way to tell if the user entered a second name. This is what I have so far:
public void makename() { // makes an inputbox to ask their name fullname = JOptionPane.showInputDialog("enter your firstname followed by a space then your second name " ); //separates the first and second name into 2 strings in order to make the username
[code]....
The problem is it accepts anything I type in without causing an error. What should I type in to stop this? ive tried anything but I cant find a way to tell if a surname has been entered or not.
Often times i don't remember which package a specific class (like ArrayList) belongs to. Is there an easy way to find that java.util.* is what i need to import, if i wanted to use the class ArrayList ?
I'm trying to find the median of a set of numbers inputted into an array and I wanted to know how I would pass the array to the median calculator after I use selection sort to organize the numbers.THIS IS THE CODE THAT ORGANIZES THE ARRAY:
public void selectionSort(double[] myArray) { for (int i = 0; i < myArray.length - 1; i++) { int index = i; for (int j = i + 1; j < myArray.length; j++)
if (myArray[j] < myArray[index])
[code]....
The code that finds the median will only work if the array being used is already organized. How do I pass the new sorted array to the code that finds the median?
I am writing a code based on the following question: "Write a method called arrayMin that accepts as an argument an array of numbers and returns the minimum value of the numbers in that array.Create an array to test your code with and call the method from main to print the min to the screen".
I cannot seem to get the code to calculate the minimum number.
Here is my progress thus far:
import java.util.*; public class Lab11q3 { public static void main(String[] args) { Scanner console = new Scanner(System.in); int number;
class triangle { public static void main (String[] args) { System.out.println("Provide three side lengths - 000 to terminate."); int a = In.getInt(); int b = In.getInt(); int c = In.getInt();
[code]....
My problem is that when I enter 5,2,5 it should be isosceles and acute but it comes out as isosceles and obtuse, and when I type 5,5,5 it comes out equilateral and right. The only one that works is if I enter 3,5,4 it will come out as scalene and right. I been at this for a while and my math looks correct.
For my project I have to write a piece of code where it returns true or false value depending whether the indicated cell is on edge of a rectangular grid (true) or not (false). Any algorithm for the following method: public static boolean onEdge (int n, int numRows, int numCols)?? To highlight, numRows and numCols and n start from 0.
I'm almost finished with my assignment, I'm just having one small issue. The goal of this assignment is to have a user input as many values they want(up to 10) into a list. I need to store those in an array then figure out the sum of the numbers, the average, the Largest value, and the smallest value. I get everything to print out correct so far except for the Smallest number, it is returning a 0 every time. Here is my code:
import java.util.Scanner; public class SimpleList{ Scanner in = new Scanner(System.in); final private static int ARRAY_LENGTH = 10; private float[] List = new float[ARRAY_LENGTH]; private int count = 0;
I'm trying to apply a subset problem without using O(n^2) complexity. I need to find the smallest possible sum between 2 sets of 2 numbers in a lists so n1 + n2 = n3 + n4 = sum.
I have found solutions that all involve having the sum first and just finding the two pairs which involves using a hash table and then taking each number and subtracting it off the sum and looking for possible combinations.
How to do it if nothing but the list of numbers is given. So no sum and no hint of the pairs.
I put this in the Java section because I'm doing it in Java and there seems to be no generic algorithm/programming Topic area.
I have written the following code to try and find a factorian but it doesn't work all the time. A factorian is supposed to be the sum of the factorials.
public static boolean isFactorion(int n){ boolean rv = false; int sum =0; int fact = 1; for(int i = n;i>=1;i--){ fact = fact * i;
I tried out doing number (generated randomly) != (another number) but that does not work. If I for example want a number between 1 and 10, but I do not want the number 5, what can I do in order to make this happen?
Program is to list all prime numbers between two entered numbers.
import java.util.Scanner; public class question6 { public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.println("Enter lower int:"); int x = input.nextInt();
I'm trying to create a class that takes an String from a Stack and checking if it's a palindrome than taking a another String from a queue and checking if that is also a palindrome.
import java.util.Stack; public class Palindrome { public static void main(String[] args) { // TODO Auto-generated method stub String enteredLine; int leftStack, rightStack; int leftQueue, rightQueue; PalinedromeArray stack1 = new PalinedromeArray();