Need help w/ java code - java

Problem "Write a method isMultiple that determines, for a pair of integers , whether the second integer is a multiple of the first. The method should take two integer arguments and return true if the second is a multiple of the first and false otherwise. [Hint: Use the remainder operator .] Incorporate this method into an application that inputs a series of pairs of integers (one pair at a time) and determines whether the second value in each pair is a multiple of the first.har()"
Keep getting "The value of your output is incorrect." Have tried doing multiple things to fix but not sure what's wrong. When I click for feedback I get
Expected Output:
·Enter·one·number:Enter·a·second·number:9·is·a·multiple·of·3↵
Do·you·want·to·enter·another·pair(y/n)?Enter·one·number:Enter·a·second·number:99·is·a·multiple·of·11↵
Do·you·want·to·enter·another·pair(y/n)?Enter·one·number:Enter·a·second·number:7·is·a·multiple·of·7↵
Do·you·want·to·enter·another·pair(y/n)?Enter·one·number:Enter·a·second·number:3·is·not·a·multiple·of·9↵
Do·you·want·to·enter·another·pair(y/n)?↵
Actual Output:
·Enter·one·number:Enter·a·second·number:9·is·a·multiple·of·3↵
Do·you·want·to·enter·another·pair(y/n)?↵
Enter·one·number:
Any help will be GREATLY appreciated ^_^
Code I have so far:
import java.util.*;
public class Multiples {
public static void main(String [] args){
boolean run = true;
while(run = true){
Scanner input = new Scanner(System.in);
System.out.print("Enter one number:");
int num1 = input.nextInt();
System.out.print("Enter a second number:");
int num2 = input.nextInt();
boolean result = isMultiple(num1,num2);
if(result = true){
System.out.println(num2 + " is a multiple of " + num1);
}
else{
System.out.println(num2 + " is not a multiple of " + num1);
}
System.out.println("Do you want to enter another pair(y/n)?");
String a = input.next();
if(YesOrNo(a)){
break;
}
}
}
public static boolean YesOrNo(String a){
if(a.equals("y"))
return false;
else if(a.equals("n"))
return true;
else
return true;
}
public static boolean isMultiple (int x , int y){
if(x % y == 0 || y % x == 0)
return true;
else
return false;
}
}

When you are checking you have to do it like this:
if(result == true){

Related

I have a question with the logic of a program that checks if a number is prime. (I have to use 3 methods)

I'm having trouble implementing 3 methods to create a program that checks if a number is prime. When I call my methods and run the program the only value that shows is 0. I'm assuming this has to do with variables or logic in my methods
I have tried using different variables to store user input then using that variable as an argument in my methods.
package practice;
import java.util.Scanner;
public class Practice {
static Scanner s = new Scanner(System.in);
public static void main(String[] args) {
//int result = 0 ; //stores number user picks
int numPicked = 0; //
int endResults = 0; //stores result of calculation
// Calling methods
isPrime(numPicked);
pickedNum(numPicked);
results(endResults);
}
// Method to check if numbers are prime
public static boolean isPrime(int numPicked){
if (numPicked <= 1) {
return false;
}
for (int i = 2; i <= numPicked; i++) {
if (numPicked % i == 0) {
return false;
}
}
return true;
}
// Method that asks user for a positive integer
public static int pickedNum (int userNumbers){
Scanner s = new Scanner(System.in);
System.out.println("Type a positive number that you want to know if it's prime or not.");
int numPicked = s.nextInt();
return numPicked;
}
// Method that displays result of calculation
public static int results (int numPicked){
if(numPicked == 0 && numPicked != 1 ){
System.out.println( numPicked + " is a Prime Number");
}
else{
System.out.println(numPicked + " is Not a Prime Number");
}
return numPicked;
}
}
I need to fix the logic within my methods to be able to call them correctly.
I did some correction in your code and working version is below. There is a lot of things that should be also changed but I tried to make your program working without big changes.
So first of all, You had 2 Scanners so I deleted one of them. Second thing your isPrime method was not working correctly so I replace it with working version. Other thing was that you called isPrime before loading value which was main problem that you always got 0. Other thing is that java is pass by value it means that if you put int as argument and you modify it inside method you still have old value so you have to assign returned value to your value again
static Scanner s = new Scanner(System.in);
public static void main(String[] args) {
//int result = 0 ; //stores number user picks
int numPicked = 0; //
boolean isPrime = false;
// Calling methods
numPicked = pickedNum();
isPrime = isPrime(numPicked);
results(numPicked, isPrime);
}
// Method to check if numbers are prime
public static boolean isPrime(int numPicked) {
if (numPicked == 2)
return true;
if (numPicked < 2 || numPicked % 2 == 0)
return false;
for (int i = 3; i * i <= numPicked; i += 2)
if (numPicked % i == 0)
return false;
return true;
}
// Method that asks user for a positive integer
public static int pickedNum()
{
System.out.println("Type a positive number that you want to know if it's prime or not.");
int numPicked = s.nextInt();
return numPicked;
}
// Method that displays result of calculation
public static int results (int numPicked, boolean isPrime)
{
if(isPrime)
{
System.out.println( numPicked + " is a Prime Number");
}
else
{
System.out.println(numPicked + " is Not a Prime Number");
}
return numPicked;
}
package test;
import java.util.Scanner;
public class Practice {
static Scanner s = new Scanner(System.in);
public static void main(String[] args) {
int numPicked = 0; //
Commented out endResults as it currently has no functionality.
//int endResults = 0; //stores result of calculation
Added a Boolean to record whether isPrime is true or false
boolean isPrime;
numPicked now stores the returned value of the pickedNum method and it takes no parameters.
// Calling methods
numPicked = pickedNum();
isPrime = isPrime(numPicked);
Pass the Boolean value to the results method, true or false.
results(numPicked, isPrime);
}
// Method to check if numbers are prime
public static boolean isPrime(int numPicked)
{
if (numPicked <= 1) {
return false;
}
Removed <= with just less than. This is as if numPicked equals its own value it will return false. In reality a prime number can only be divided by itself so you just need to check the numbers less than it.
for (int i = 2; i < numPicked; i++) {
if (numPicked % i == 0) {
return false;
}
}
return true;
}
// Method that asks user for a positive integer
public static int pickedNum ()
{
Scanner s = new Scanner(System.in);
System.out.println("Type a positive number that you want to know if it's prime or not.");
int numPicked = s.nextInt();
return numPicked;
}
// Method that displays result of calculation
public static int results (int numPicked, boolean isPrime)
{
Checks if isPrime is true.
if(isPrime)
{
System.out.println( numPicked + " is a Prime Number");
}
else
{
System.out.println(numPicked + " is Not a Prime Number");
}
return numPicked;
}
}

Trying to get isMultiple to execute properly

this executes, however it returns all answers as positive "8 is a multiple of 15" even if it is false. Not sure what I'm not seeing.
Here is what i have:
import java.util.*;
public class Multiples {
public static void main(String [] args){
boolean run = true;
while(run = true){
Scanner input = new Scanner(System.in);
System.out.print("Enter one number:");
int num1 = input.nextInt();
System.out.print("Enter a second number:");
int num2 = input.nextInt();
boolean result = isMultiple(num1,num2);
if(result = true){
System.out.println(num2 + " is a multiple of " + num1);
}
else{
System.out.println(num2 + " is not a multiple of " + num1);
}
System.out.println("Do you want to enter another pair(y/n)?");
String a = input.next();
if(YesOrNo(a)){
break;
}
}
}
public static boolean YesOrNo(String a){
if(a.equals("y"))
return false;
else if(a.equals("n"))
return true;
else
return true;
}
public static boolean isMultiple (int x , int y){
if(x % y == 0 || y % x == 0)
return true;
else
return false;
}
}
if(result = true){
replace with
if(result == true){
(or simply)
if(result){
You are assigning it instead of comparing.

Are my loops preventing me from getting the expected output?

My goal is to determine, for a pair of integers, whether the second integer is a multiple of the first. My code is as follows:
import java.util.Scanner;
public class Multiples {
public static void main(String [] args) {
boolean run = true;
while(run) {
Scanner input = new Scanner(System.in);
System.out.print("Enter one number:");
int num1 = input.nextInt();
System.out.print("Enter a second number:");
int num2 = input.nextInt();
boolean result = isMultiple(num1, num2);
if(result) {
System.out.println(num2 + " is a multiple of " + num1);
} else {
System.out.println(num2 + " is not a multiple of " + num1);
}
System.out.print("Do you want to enter another pair(y/n)?");
run = YesOrNo(input.next());
}
}
public static boolean YesOrNo(String a) {
if(a.equals("y"))
return true;
else
return false;
}
public static boolean isMultiple (int x , int y) {
if(y % x == 0)
return true;
else
return false;
}
}
This is my output:
This is the expected output:
The expected output is confusing me.

Find perfect squares

I am trying to write a loop that calls a method to determine if a number entered is a perfect square. It compiles just fine so I must have a logic error, for the life of me though I can't find it. No matter the number I put in it seems to always return false, leading me to believe the problem lies inside the isPerfect() method. I however, do not know enough about java yet to know where to go from here. Here's the code I've got so far:
public class Square
{
public static void main(String[] args)
{
int input = 0; //The default value for input
Scanner keyboard = new Scanner(System.in);
while (input != -1)
{
System.out.println("Please enter a number, or enter -1 to quit");
input = keyboard.nextInt();
if (isPerfect(input) == true) //Call isPerfect() to determine if is a perfect square
{
System.out.println(input + " is a perfect square.");
}
else if(input == -1) //If equals exit code, quit
{
break;
}
else //If it is not a perfect square... it's not a perfect square
{
System.out.println(input + " is not a perfect square.");
}
}
}
public static boolean isPerfect(int input)
{
double num = Math.sqrt(input); //Take the square root of the number passed
if (((num * num) == input) && (num%1 == 1)) //If the number passed = it's roots AND has no remainder, it must be a perfect sqaure
{
return true; //Return true to the call
}
else
{
return false; //Return false to the call
}
}
}
Two potential issues.
arithmetic with doubles is inaccurate. You probably want
int num = Math.round(Math.sqrt(input));
Your test for no remainder doesn't do what you think ... (num%1 == 1) just tests whether n is odd. And you dont really need it .. all you need is if( num*num == input) { ... }
So with the program fixed, here is the entirety of the code:
import java.util.Scanner;
public class Square
{
public static void main(String[] args)
{
int input = 0; //The default value for input
Scanner keyboard = new Scanner(System.in);
while (input != -1)
{
System.out.println("Please enter a number, or enter -1 to quit");
input = keyboard.nextInt();
if (isPerfect(input) == true) //Call isPerfect() to determine if is a perfect square
{
System.out.println(input + " is a perfect square.");
}
else if(input == -1) //If equals exit code, quit
{
System.out.println("Breaking!");
break;
}
else //If it is not a perfect square... it's not a perfect square
{
System.out.println(input + " is not a perfect square.");
}
}
System.out.println("Main complete!");
}
/**
The isPerfect() method returns whether or not a number is a perfect square.
#param input The input from the keyboard scanner, passed as an argument
*/
public static boolean isPerfect(int input)
{
int num = ((int)Math.sqrt(input)); //Take the square root of the number passed, as an integer
if (num*num == input) //If the number passed = it's roots AND has no remainder, it must be a perfect sqaure
{
return true; //Return true to the call
}
else
{
return false; //Return false to the call
}
}
}
import java.util.Scanner;
class perfect
{
public static void main(String args[])
{
int count=0;
System.out.println ("enter any number");
Scanner in =new Scanner(System.in);
int n=in.nextInt();
for(int i=1;i<n;i++)
{
if(n>i*i)
{
count++;
System.out.println( i*i);
}
}
System.out.println("there are "+ count + " perfect numbers");
}
}

Maths Game - returning points in methods

So my objective is to create a maths game where the user selects if he/she wants a maths question from a file or a random generate one consisting of the 4 maths elements in 3 difficulties.I have created a lot of methods... I have an idea where im going but now im stuck. I need to have it so it keeps a score of questions answered correctly. How do i return the points to the main method and have the game going until the user presses 3 on the gamePlay()method
public class MathsGameProject2 {
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
int score;
int points = 0;
int questionType;
System.out.print("Please enter the what type of question you want" + "\n 1 Question from a file" + "\n 2 Random question" + "\n 3 Quit game\n");
questionType = keyboard.nextInt();
while (questionType != 3) {
if (questionType == 1) {
questionFromFile();
} else if (questionType == 2) {
randomQuestion();
} else {
System.out.println("Please enter the what type of question you want" + "\n 1 Question from a file" + "\n 2 Random question" + "\n 3 Quit game\n");
}
}
}
public static questionFromFile() {
}
public static randomQuestion() {
Scanner keyboard = new Scanner(System.in);
int difficulty;
System.out.println("Please enter the difficulty you want to play." + "\n 1. Easy" + "\n 2. Medium" + "\n 3. Hard\n");
difficulty = keyboard.nextInt();
if (difficulty == 1) {
easy();
} else if (difficulty == 2) {
medium();
} else if (difficulty == 3) {
hard();
} else {
System.out.println("Please enter a number between 1-3\n");
}
}
public static easy() {
Scanner keyboard = new Scanner(System.in);
int mathElement;
System.out.print("What element of maths do you want?" + "\n1 Additon" + "\n2 Subtraction" + "\n3 Multiplication" + "\n4 Division\n");
mathElement = keyboard.nextInt();
if (mathElement == 1) {
easyAdd();
} else if (mathElement == 2) {
easySub();
} else if (mathElement == 3) {
easyMulti();
} else if (mathElement == 4) {
easyDiv();
} else {
System.out.println("Please enter a number between 1-4\n");
}
}
public static easyAdd() {
Scanner keyboard = new Scanner(System.in);
Random rand = new Random();
int num = rand.nextInt(10) + 1;
int num2 = rand.nextInt(10) + 1;
int correct = num + num2;
int answer;
System.out.print("What is the answer of " + num + " + " + num2 + " ?");
answer = keyboard.nextInt();
if (answer == correct) {
}
}
In order to keep track of how many questions the user answers successfully, you will need to:
For each question, return whether or not the user answered correctly
Have a counter which increments whenever a user answers a question correctly
Optionally, have a counter which increments whenever a question is answered wrong
For #1, you can use a boolean return value for specifying if the question was answered successfully.
return (answer == correct);
You will want to propagate that return value all the way up to the main() method.
static void main() {
....
boolean isCorrect = randomQuestion();
....
}
static boolean randomQuestion() {
....
return easy();
....
}
static boolean easy() {
....
return easyAdd();
....
}
static boolean easyAdd() {
...
return (answer == correct);
}
Then for #2 and #3, you can increment counter(s) defined in main based on the value returned by randomQuestion()
int numberCorrect = 0;
int numberWrong = 0;
....
boolean isCorrect = randomQuestion();
if (isCorrect) {
numberCorrect++;
} else {
numberIncorrect++;
}
Additionally (no pun intended), you can use a while loop to continuously receive user input until you get your exit code, which in this case is 3. One way to do this is to use a while(true) loop and break out when the user enters 3.
while (true) {
/* Get user input */
....
if (questionType == 3) {
break;
}
}
Finally, after your loop, you can simply print out the value of your numberCorrect and numberIncorrect counters.
Hope this helps.

Categories