Beginner to java and not sure what this means - java

import java.util.*;
public class decimalToBinaryTest {
public static void main(String[] args) {
int number;
Scanner in = new Scanner(System.in);
System.out.println("Enter a positive interger");
number = in.nextInt();
if (number < 0) {
System.out.println("Not a positive interger");
}
else {
System.out.print("Convert to binary is: ");
System.out.print(binaryform(number) + ".");
}
}
private static Object binaryform(int number) {
int remainder;
if (number <= 1) {
System.out.print(number);
return null;
}
remainder = number % 2;
binaryform(number >> 1);
System.out.print(remainder);
{
return " ";
}
}
}
In the main part of a program an int variable was created. In the next part is says private static Object binaryform ( int number ). Is the int number in the Objectrelating to the variable I the main method?

Yes and no. The variable name number has nothing to do with the main method. It is a formal parameter to the method named binaryform. The parameter number only exists within the method itself. However, when binaryform is called, the actual value of the variable (or constant) used in the call becomes the value of number while the method is executing.
public class Example {
public static void main(String[] args) {
int n = 3; // the name could be "number" and no behavior would change
Object bf = binaryForm(n);
// do something with bf
}
private static Object binaryform(int number) {
// from the call above, number will have the value 3
Object o = . . .;
// generate or modify o from the value of number
return o;
}
}

Related

I am trying to write a recursive function that will return the result of the sum (integer) and takes one argument

I am trying to follow these instructions:
Write a program that will ask the user for the value of an integer n, and compute the sum 1 + 2 + 3 + 4 + ... + n. The requirement for this lab is that you write a recursive function that returns the result of the sum (integer) and takes one argument, n, of type integer. You will then call the function and print out its results as follows:
int mysum = recursive_addition(n);
System.out.println("The sum 1+2+...+n is: "+ mysum);
the problem is on line 20 because of the error below
Main.java:20: error: method main (String[]) Is already defined in class Main
private static void main (String args[])
^
Here is my code:
import java.util.Scanner;
public class Main {
public static void main(String[] args){
Scanner s = new Scanner(System.in);
System.out.print("Please enter a integer: ");
int n = s.nextInt();
int mysum = recurSum(n);
System.out.println("The sum 1+2+3+4 is :" + n );
}
public static int recurSum(int n)
{
if (n <= 0)
return n;
return n + recurSum(n - 1);
}
public static void main (String args[])
{
int n = 5;
System.out.println(recurSum(n));
}
}
As the compiler stated, you are duplicating the main entry points of your application.
Just remove the last main function and modify as follow
Here is the working code
import java.util.Scanner;
public class Main {
public static void main(String[] args){
Scanner s = new Scanner(System.in);
System.out.print("Please enter a integer: ");
int n = 5; // Just put there the number
int mysum = recurSum(n);
System.out.println("The sum 1+2+3+4 is :" + n );
}
public static int recurSum(int n)
{
if (n <= 0)
return n;
return n + recurSum(n - 1);
}
}

Recursive methods which exclude each other?

I am trying to write a method that calculates the sum of odd integers between 1 and a given positive integer n, without using anything else than if statements (sheesh!). It worked out just fine until I decided to also create a method that would ask recursively for the number until it was positive and use it to get n.
Now my program outputs the correct results until I enter a negative number. It then asks for a postive one until I enter one and it outputs 0, the value I initialised the variable val with.
I'm not sure where the logic error is. Could you please take a look? I'm sure it's something obvious, but I guess I have just reached the end of my wits today. Thanks!
package oddsum;
import java.util.Scanner;
public class Oddsum {
public static int oddSum(int n){
int val=0;
if(n>1){
if(n%2==0){
val=n+oddSum(n-1);
}else{
val=oddSum(n-1);
}
}
return val;
}
public static int request(int n){
Scanner in= new Scanner(System.in);
System.out.println("Give me a positive integer: ");
n=in.nextInt();
if (n<0){
System.out.println("I said positive! ");
request(n);
}
return n;
}
public static void main(String[] args) {
int val=0;
int n=request(val);
System.out.println(oddSum(n));
}
}
You should remove input parameter from your request() method. Because your negative input is carried out through the recursive call.
public class Oddsum {
public static int oddSum(int n) {
int val = 0;
if (n > 1) {
if (n % 2 == 0) {
val = n + oddSum(n - 1);
} else {
val = oddSum(n - 1);
}
}
return val;
}
public static int request() {
Scanner in = new Scanner(System.in);
System.out.println("Give me a positive integer: ");
int n = in.nextInt();
if (n < 0) {
System.out.println("I said positive! ");
return request();
}
return n;
}
public static void main(String[] args) {
int n = request();
System.out.println(oddSum(n));
}
}
Output;

Java for/while loop for unique integer

Need help figuring out what's wrong with my code. I am taking a
beginning java course so I have to use simple code.
an integer N is unique if there exists an integer i such that: N = i*i
+ i Here are some examples of unique numbers: 2 (because 2 = 1*1 + 1) 6 (because 6 = 2*2 + 2) 12 (because 12 = 3*3 + 3)
Asks the user to enter an integer N. Prints out whether that number is
a unique number. If not print not a unique number
import java.util.Scanner;
public class task4 {
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.printf("Enter an integer N: ");
int N = in.nextInt();
boolean unique = true;
int i = 1;
while ((i*i)+i == N)
{
if ((i*i)+i != N)
{
unique = false;
i++;
}
}
if (unique)
{
System.out.printf("%d is unique.\n",N);
}
else
{
System.out.printf("%d is not unique.\n", N);
}
}
}
Your while loop will only evaluate if 1*1 + 1 == N is true. I don't want to give you the answer directly so I will give you a hint: how can you change the while loop to return true for every value up to N?
Also try redoing the inside of the while loop. Think about how you can make it to make unique true if any value of i is valid.
This should be of some hlep to you. Check for i should always be lesser than N in the while loop. At least it has got some exit condition (which is logical). In your case there is no exit condition out of while loop when your (i*i)+i grows beyond N.
import java.util.Scanner;
public class Task4 {
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.printf("Enter an integer N: ");
int N = in.nextInt();
boolean unique = false;
int i = 1;
while (((i*i)+i) <= N)
{
if ((i*i)+i != N) {
i++;
} else {
unique = true;
break;
}
}
if (unique)
{
System.out.printf("%d is unique.\n",N);
}
else
{
System.out.printf("%d is not unique.\n", N);
}
}
}
Maybe you can try this:
import java.util.Scanner;
public class task4 {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.printf("Enter an integer N: ");
int N = in.nextInt();
boolean unique = true;
double sd = Math.sqrt(N);
double fd = Math.floor(sd);
if(sd * (sd+1) != N){
unique = false;
}
if (unique)
{
System.out.printf("%d is unique.\n",N);
}
else
{
System.out.printf("%d is not unique.\n", N);
}
}
}

How to invoke multiple method signatures within a main method?

So I have three required codes I have already figured out. They are the codes for a quadratic formula, codes for an ISBN checker, and a code for Newtons Method. I'm supposed to make a menu with options 1, 2, and three each containing these codes respectively.
I guess this means I need different methods for this right? I was never really taught - I was told we had to always put in public class void main (String []args){ for everything, and I was just told there were variations to this?
For Quadratics formula, the information is : Return - void and parameters of three doubles, Newtons method: Return - double and parameters of 1 double, and ISBN checker: Return: Boolean and Parameters of 1 string. I don't really understand the parameters thing either. Help would be appreciated. I know this aesthetically looks horrible, but because my codes for now are relatively short I just edit the style when I' done. I know a lot of things are wrong in this too and I've spent time trying to figure them out.
import Java.util.Scanner;
public class HelperMethod{
public static void main(String[] args) {
Scanner userInputScanner = new Scanner (System.in);
System.out.println ("You have three options. press one for the quadratic Formula, 2 for the newtons Method, and 3 for an ISBN checker.");
int input = userInputScanner.nextInt();
if (input = 1){
}else if (input = 2) {
private class NewtonsMethod {
public static void NewtonsMethod(String[] args) {
Scanner userInputScanner = new Scanner (System.in);
double guess, fX, fPrimeX, newGuess;
System.out.println ("enter in a value give");
guess = userInputScanner.nextDouble();
System.out.println ("Your guess is " + guess);
while (true) {
fX = (6 * Math.pow (guess,4)) - (13 * Math.pow (guess,3)) - (18 * Math.pow (guess,2)) + (7 * guess) + 6;
fPrimeX = (24 * Math.pow (guess,3)) - (39 * Math.pow (guess,2)) - 36 * guess + 7;
newGuess = guess - (fX / fPrimeX);
System.out.println ("A possible root is " + newGuess);
if (Math.abs(newGuess - guess) < 0.00001) {
break;
} else {
guess = newGuess;
}
}
System.out.println ("The root is: " + newGuess);
}
}
}else{
private class BookNumber {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
char f;
int e, g, h;
int result = 0;
System.out.println ("Pleas enter a thirteen digit number");
String a = scanner.nextLine();
if (a.length() == 13){
for (int i = 0; i < 13; i ++) {
f = a.charAt(i);
e = Character.digit(f, 10);
if (i % 2 == 0) {
g = e * 1;
result = result + g;
} else {
g = e * 3;
result = result + g;
}
}
System.out.println ("The added sum of you numbers is " + result);
if (result % 10 == 0) {
System.out.println ("This combination IS a ISBN number");
} else {
System.out.println ("This is NOT an ISBN number");
}
} else {
System.out.println ("This combination is not thirteen digits long");
}
}
}
}
}
}
First of all, right now you're setting input to 1 in your first if statement. To compare input to 1 instead, use the == operator, i.e. if (input == 1) {.... Secondly, you don't really need to use classes, you can simply have methods NewtonsMethod(), BookNumber() etc. and run them when the input is correct.
public class HelperMethod{
public static void main(String[] args) {
int input;
//Handle user input
switch (input) {
case 1:
newtonsMethod();
break;
case 2:
bookNumber();
break;
case 3:
anotherMethod();
break;
}
}
public static void newtonsMethod() {
//Your code
}
public static void bookNumber() {
//Your code
}
public static void anotherMethod() {
//Your code
}
}
Methods should never be inside one another. That is what classes are for. A method is an element within a class. For example, in your case your class was named "HelperMethod". Your methods need to begin after the main method's code block is closed with a curly brace "}"
as an example
// This would be your main method.
public static void main(String args[]) {
// Your method is CALLED here.
someMethod();
{
// Then this is where your next method would start.
public static void someMethod() {
// Your code for the method of course goes here.
}
Of course you need your class setup and needed imports ABOVE the main method but you have that setup correctly already. With this setup, it makes it easy to call public methods that are in other classes. Your private methods are not really needed unless you intend to use more than one class, at which point you will need to import that class and then call the method like so
SomeClass.someMethod();

How take user input in one method and use it in another

I am very new to java and have searched around and can't seem to find what to do. I need to take int number and be able to use it in another method. I have to use two methods to do this. I am unsure how to call upon it.
public static void first()
{
System.out.print("Enter number: ")
Scanner scan = new Scanner(System.in);
int number = scan.nextInt();
}
public static void getNumber(String name, int move)
{
if (number == 1)
{
System.out.println("Player shows one" );
}
Make a method which return a number and call it from another method.
public static int first()
{
System.out.print("Enter number: ")
Scanner scan = new Scanner(System.in);
int number = scan.nextInt();
return number;
}
public static void getNumber(String name, int move)
{
int number = first(); //Call method here.
if (number == 1)
{
System.out.println("Player shows one" );
}
}
Define number as a class attribute.
Something like (its not final/working code)
class myClass{
int number = 3; // Or any other default value
public static void first()
{
//....
obj.number = scan.nextInt();
//...
}
public static void getNumber(String name, int move)
{
if (obj.number == 1)
{
//......
}
}
}
After int number call method
int number = scan.nextInt();
getNumber("Your Name", number);

Categories