Please tell me how to call recall method.
I am new in Java.
I am making a program to display prime and composite number.
package composite;
import java.util.Scanner;
public class composite {
public static void main(String[] args) {
Scanner p = new Scanner(System.in);
System.out.println("press number till you want composite number & prime numbers");
int m=p.nextInt();int g[]=new int [m+1];prime(m);
for(int k=4;k<=m;k++)
{
for(int b=2;b<k;b++){
if(k%b==0){
g[k]=k;break;
}
}
}
}
public static int prime(int m){
int e[]= new int[m+1];
for(int i=2;i<m;i++)
{
int p=0;
for(int j=2;j<i;j++)
{
if(i%j==0)
p=1;
}
if(p==0)
e[i]=i;
}return(m);
}
public static int recall(int m, int [] e, int [] g){
for(int a=1;a<m;a++){
System.out.println(e[a]+" "+g[a]);
}
return m;
}
}
Take a look at following and modify the variables accordingly :
Composite call_recall = new Composite();
int recall_value = recall(m,e,g);
Place the proper values as you require in place of m, e and g.
Related
The numThrows Variable is inciting an error of variable not found when used in the main method. even though i declare it in one of the methods.
I use the declare the variable in the void prompt method. This program is designed to calculate Pi using random coordinates then uses a formula to estimate pie over a user given amount of tries.
import java.util.Random;
import java.util.Scanner;
import java.io.PrintWriter;
import java.io.File;
import java.io.IOException;
public class Darts
public static void prompt()
{
Scanner in = new Scanner(System.in);
System.out.println("How many throws per trial would you like to do?: ");
int numThrows = in.nextInt();
System.out.println("How many trials would you like to do?: ");
int numTrials = in.nextInt();
}
public static double[] randomX( int numThrows)
{
int darts = 0;
int i = 0;
double[] cordX = new double[numThrows];
while(darts <= numThrows)
{
cordX[i] = Math.random();
i++;
}
return cordX;
}
public static double[]randomY(int numThrows)
{
int darts = 0;
int i = 0;
double [] cordY = new double[numThrows];
while(darts <= numThrows)
{
cordY[i] = Math.random();
i++;
}
return cordY;
}
public static void getHits(int numThrows, double[] cordX, double[] cordY)
{
int ii = 0;
int i = 0;
double hits = 0;
double misses = 0;
for(i = 0; i <= numThrows; i++)
{
if( Math.pow(cordX[ii],2) + Math.pow(cordY[ii],2) <= 1)
{
hits++;
ii++;
}
else{
misses++;
}
}
}
public static double calcPi(int misses, int hits)
{
int total = hits + misses;
double pi = 4 * (hits / total);
}
// public static void print(double pi, int numThrows)
// {
// System.out.printf(" %-7s %3.1f %7s\n", "Trial[//numtrial]: pi = "
// }
public static void main(String[] args)throws IOException
{
prompt();
double[] cordX = randomX(numThrows);
double[] cordY = randomY(numThrows);
gethits();
double pi = calcPi(misses, hits);
}
}
If numThrows is declared within another function, then its scope does not extend to the main method.
Instead, if you want to use it in both the main method and the other one, make it a class instance.
For example:
class SomeClass {
public static int numThrows = 2;
public static void test() {
numThrows = 4; // it works!
}
public static void main(String[] args) {
System.out.println(numThrows); // it works!
}
}
Therefore, its scope will be extended to all the members of the class, not just the method.
numThrows is an instance variable to your prompt method. If you want to do what I think you want to do, make numThrows a static variable outside any methods.
It will look like this:
public class Darts {
public static int numThrows
public static int numTrials
These variables can be referenced from any method. This should fix it.
Try to remove the method prompt() it's unused, and put his block in the main method.
public static void main(String[] args)throws IOException
{
Scanner in = new Scanner(System.in);
System.out.println("How many throws per trial would you like to do?: ");
int numThrows = in.nextInt();
System.out.println("How many trials would you like to do?: ");
int numTrials = in.nextInt();
double[] cordX = randomX(numThrows);
...
I am working on a problem for 5 hours, and I searched in a book and on the Internet, but I still cannot solve this problem, so please help me to check what's wrong with the program. And the pic is the requirement for this program.
//imports
import java.util.Scanner;
import java.util.Random;
public class Lab09 // Class Defintion
{
public static void main(String[] arugs)// Begin Main Method
{
// Local variables
final int SIZE = 20; // Size of the array
int integers[] = new int[SIZE]; // Reserve memory locations to store
// integers
int RanNum;
Random generator = new Random();
final char FLAG = 'N';
char prompt;
prompt = 'Y';
Scanner scan = new Scanner(System.in);
// while (prompt != FLAG);
// {
// Get letters from User
for (int index = 0; index < SIZE; index++) // For loop to store letters
{
System.out.print("Please enter the number #" + (index + 1) + ": ");
integers[index] = RanNum(1, 10);
}
// call the printStars method to print out the stars
// printArray(int intergers, SIZE);
} // End Main method
/***** Method 1 Section ******/
public static int RanNum(int index, int SIZE);
{
RanNum = generator.nextInt(10) + 1;
return RanNum;
} // End RanNum
/***** Method 2 Section ******/
public static void printArray(int integers, int SIZE) {
// Print the result
for (int index = SIZE - 1; index >= 0; index--) {
System.out.print(integers[index] + " ");
}
} // End print integers
} // End Lab09
As Tim Biegeleisen and Kayaman said, you should put everything in the question and not just an external image.
You have a lot of errors in your code. Below the code will compile and run but I recommend you to take a look and understand what it has been done.
Errors:
If you are declaring a method, make sure you use { at the end of the declaration. You have:
public static int RanNum(int index, int SIZE);
Should be:
public static int RanNum(int index, int SIZE){
// Code here
}
You also should declare outside your main method so they can be accessed across the program.
If you are passing arrays as arguments, in your method the parameter should be an array type too.
You have:
public static void printArray(int integers, int SIZE) {
// Code her
}
Should be
public static void printArray(int[] integers, int SIZE) {
// Code her
}
Here is the complete code:
package test;
import java.util.Random;
import he java.util.Scanner;
public class Test {
//Local variables
public static final int SIZE = 20; //Size of the array
static int integers[] = new int[SIZE]; //Reserve memory locations to store integers
static int randomNumber;
static Random generator = new Random();
static String prompt;
static final String p = "yes";
static boolean repeat = true;
static Scanner input = new Scanner(System.in);
Test() {
}
/***** Method 1 Section ******/
public static int RanNum (int low, int high) {
randomNumber = generator.nextInt(high-low) + low;
return randomNumber;
} //End RanNum
/***** Method 2 Section ******/
public static void printArray(int[] intArray, int SIZE) {
//Print the result
for (int i = 0; i < SIZE; i++) {
System.out.print (intArray[i] + " ");
}
} //End print integers
public static void main (String [] arugs) {
do {
for (int i = 0; i < SIZE; i++) {
integers[i] = RanNum(1, 10);
}
printArray(integers, SIZE);
System.out.println("Do you want to generate another set of random numbers? Yes/No");
prompt = input.nextLine();
} while(prompt.equals(p));
}
}
I wrote this code but it says statement 6 is an error
could someone tell me whats wrong please
public class arraytest{
private int a[];
private int noe; //number of elememtos
public arraytest(){
noe=5;
a[5];}
}
public void read(){
a[0]=5;a[1]=3;a[2]=6;a[3]=9;a[4]=2;}
public int sum(){
int sum=0;
for (int i=0; i<a.length();i++)
sum=sum+a[i];
return sum;}
public static void main(String[]args){
arraytest x=new arraytest();
x.read();
System.out.println("The sum is " + x.sum());
}
}
ah, Rookie mistake
He thought he initialized the array with a[5] which is wrong
I'm guessing he tried to do this
public arraytest(){
noe=5;
a = new int[noe];
}
And dude, learn how to indent your code, so that it will be much readable to others trying to help you out
public class arraytest{
private int a[];
private int noe; //number of elememtos
public arraytest(){
noe=5;
a = new int[noe];
}
public void read(){
a[0]=5;a[1]=3;a[2]=6;a[3]=9;a[4]=2;
}
public int sum(){
int sum=0;
for (int i=0; i<a.length;i++)
sum=sum+a[i];
return sum;
}
public static void main(String[]args){
arraytest x=new arraytest();
x.read();
System.out.println("The sum is " + x.sum());
}
}
a[5];
Is not a valid statement. You need to perform some assignment.
a[5] = 5; //for example
Is there a hack to print the first n fibonacci numbers without calling a loop
for(int i=1; i<n; i++)
System.out.println(computeF(n));
from the main program?
public static int computeF(int n)
{
if(n==0)
{
return 0;
}
else if(n==1)
{
return 1;
}
else
{
return computeF(n-1)+computeF(n-2);
}
}
There might be a way to print the intermediate values in recursion which will print the fibonacci numbers.
You could use tail recursion.
public class Fid
{
static int n1=0;
static int n2=1;
static int nex=0;
public static void fb(int n)
{
if(n<10)
{
if(n==0)
{
System.out.print(" "+n);
n++;
fb(n);
}
else
if(n==1)
{
System.out.print(" "+n);
n++;
fb(n);
}
else{
nex=n1+n2;
System.out.print(" "+nex);
n1=n2;
n2=nex;
n++;
fb(n);
}
}
}
public static void main(String[] args)
{
fb(0);
}
}
using recursion:-
class FibonacciRecursion
{
private static int index = 0;
private static int stoppingPoint = 9;
public static void main (String[] args)
{
int n1 = 0;
int n2 = 1;
fibonacciSequence(n1, n2);
}
public static void fibonacciSequence(int n1, int n2)
{
System.out.println("index: " + index + " -> " + n1);
// make sure we have set an ending point so this Java recursion
// doesn't go on forever.
if (index == stoppingPoint)
return;
// make sure we increment our index so we make progress
// toward the end.
index++;
fibonacciSequence(n2, n1+n2);
}
}
//Java program to print Fibonacci Series up to n terms given by user without using loop
import java.util.* ;
public class Fibonacci
{
public static void main(String[] arguments)
{
Scanner s = new Scanner(System.in);
System.out.print("Enter the no of terms :");
int no_of_terms= s.nextInt(),a=1,b=0,c=0,count=1;
System.out.print("0 ");//printing the first term
fib(no_of_terms,a,b,c,count);}
public static void fib(int no_of_terms,int a,int b,int c,int count)
{
//when value of count will be equal to the no of terms given by user the program will terminate
if (count==no_of_terms)
System.exit(0);
else
{
count++;
System.out.print(a+" ");
c=b;
b=a;
a=b+c;//calculating the next term
fib(no_of_terms,a,b,c,count);//calling the function again with updated value
}
}
}
import java.util.*;
public class Fibonacci{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter a no.");
int n= sc.nextInt(),a=1,b=0,c=0;
num(n,a,b,c);
}
public static void num(int n,int a,int b,int c){
if(a<=n){
System.out.println(a);
c=b;
b=a;
a=b+c;
num(n,a,b,c);
}
}
}
Question:
matrix m1 = new matrix(); // should produce a matrix of 3*3
matrix m2 = new matrix(5,4); //5*4
matrix m3 = new matrix(m2); //5*4
What should be there in the copy constructor to make a new matrix m3 of the same order as of m2?
public class matrix {
int a[ ][ ];
matrix(){
a = new int[3][3];
}
matrix(int x, int y){
a= new int [x][y];
}
matrix (matrix b1){
//how to use value of x and y here....
}
void show(){
System.out.println(a.length);
for(int i=0;i<a.length;i++){
System.out.print(a[i].length);
}
}
}
public class matrixtest {
public static void main(String [ ] args){
matrix a = new matrix();
matrix b = new matrix(5,4);
matrix c = new matrix (b);
a.show();
b.show();
c.show();
}
}
NOTE: You can not use any extra instance variable except the array a.
Accepted answer: #Chankey: this(b1.a.length,b1.a[0].length); – John
Store the number of rows, and the number of columns in the matrix class, and create getters for them.
public class Matrix {
int[][] a;
int rowNum;
int colNum;
//...
public Matrix(Matrix b) {
a=new int[b.getRowNum()][b.getColNum()];
this.rowNum = b.getRowNum();
this.colNum = b.getColNum();
}
public int getRowNum() {
return this.rowNum;
}
}
You need to get the size of the passed b1 matrix
int x = b1.length;
int y = b1[0].lenght;
and can then use it to construct the final array.
a= new int [x][y];
Use
a =new int[b1.a.length][b1.a[0].length];
But it is not recommended .
you should have some get method , which return
matrix dimension.
This is homework, so I'll give you a hint:
How would you get the lengths of the 2 dimensional matrix (a[][]) of b1? proper methods in the matrix class will help - how would you implement those (getX, getY)?
Also, it is better to redirect the constructors to the most detailed one, for example:
matrix(){
this(3,3); // call the constructor below with parameters 3,3
}
matrix(int x, int y){
a= new int [x][y];
}
This can be the most probable answer without using any other instance variable other then the array itself:
import java.io.*;
class matrix
{
private int arr[][];
public matrix() //Default Constructor
{
this(3,3);
}
public matrix(int r,int c) //Parameterized Constructor
{
arr=new int[r][c];
read();
}
public matrix(matrix m) //Copy Constructor
{
System.out.println("Fetching array...");
int r,c;
r=m.arr.length;
c=m.arr[0].length;
arr=new int [r][c];
for(int i=0;i<r;i++)
{
for(int j=0;j<c;j++)
{
arr[i][j]=m.arr[i][j];
}
}
}
public void read()
{
int i,j,r,c;
r=arr.length;
c=arr[0].length;
Console con=System.console();
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
arr[i][j]=Integer.parseInt(con.readLine());
}
}
}
public void show()
{
int i,j;
for(i=0;i<arr.length;i++)
{
for(j=0;j<arr[0].length;j++)
{
System.out.print(" "+arr[i][j]);
}
System.out.println();
}
}
}