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
Related
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.
I'm working on a homework problem that is asking me to write a program that generates 20 random numbers (0-99) in an array and then sorts and prints them.
I am getting crazy errors in my methods and I can't figure out whats wrong. I keep getting errors "Illegal start of expression, ; expected and .class expected. Any advice would be great.
import java.util.Arrays;
public class P6_14
{
public static void main(String[] args)
{
System.out.println("This program will sort randomly generated numbers");
public static int[] createNumbers(int n)
{
int[] numbers = new int[n];
for (int i = 0; i < n; i++)
{
numbers[i] = (int) (Math.random() * 99 + 1);
}
return numbers;
}
public static void orderArray(int[] array)
{
Arrays.sort(array);
System.out.println(Arrays.toString(array));
}
}
}
The way your trying to fit everything into one main method isnt very OOP. Try to create methods in the future within your class! Hope this help
import java.util.Arrays;
public class Hello{
private int[] numbers = new int[20];
public Hello(){
for (int i = 0; i < this.numbers.length; i++)
{
this.numbers[i] = (int) Math.floor(Math.random()*99);
}
}
public void orderArray()
{
Arrays.sort(this.numbers);
}
public void printArray()
{
for (int i = 0; i < 20; i++)
{
System.out.println(this.numbers[i]);
}
}
public static void main(String[] args)
{
System.out.println("This program will sort randomly generated numbers1");
Hello test = new Hello();
System.out.println("Printing randomized");
test.printArray();
test.orderArray();
System.out.println("Printing sorted");
test.printArray();
}
}
I am able to create a Max Heap using a function heapify() but when i try to call it again(to delete max and create a sorted array) the program gets stuck/doesn't stop taking input. What's wrong?
Is this a memory problem?If I increase the number of calls by increasing the frequency of for loop it still works fine.
public class HeapSort
{
int[] heap;
public void sort(int length)
{
int temp;
for(int i=length;i>=1;i--)
{
heapify(i,length);
}
//if I try to call heapify again(even once) after this,the program gets stuck
}
public void heapify(int i,int l)
{
int lchild=2*i,rchild,max;
int temp;
while(lchild<=l)
{
rchild=(2*i)+1;
if(rchild<=l)
max=(heap[lchild]>heap[rchild])? lchild:rchild;
else
max=lchild;
if(heap[i]<heap[max])
{
temp=heap[i];
heap[i]=heap[max];
heap[max]=temp;
i=max;
}
lchild=2*i;
}
}
public static void main(String args[]) throws IOException
{
BufferedReader r= new BufferedReader(new InputStreamReader(System.in));
int length=Integer.parseInt(r.readLine());
HeapSort Heap=new HeapSort();
Heap.heap=new int[length+1];
for(int i=1;i<=length;i++)
Heap.heap[i]=Integer.parseInt(r.readLine());
Heap.sort(length);
for(int i=1;i<=length;i++)
System.out.print(Heap.heap[i]+" ");
}
}
Heapify should be done for length/2 iterations because it is like a tree structure.
Here is a complete code for heap sort...This sort array 's'
public class HeapSort {
public static void main(String[] args) {
String s[]={"aaaa","dddd","cccc","gggg","bbbbb"};
AsHeap(s);
HeapSort(s);
for(String x:s){
System.out.println(x);
}
}
public static void AsHeap(String s[]){
for( int i = s.length / 2; i >= 0; i-- ){
DownHeap( s, i, s.length );
}
}
public static void HeapSort(String[] s){
for(int i=s.length-1;i>0;i--){
swap(s,0,i);
DownHeap(s,0,i);
}
}
public static int getLeftChildIndex(int i){
return 2 * i + 1;
}
private static void DownHeap(String[] s, int i, int length) {
int indexOfChild;
String temp;
for(temp=s[i];getLeftChildIndex(i)<length;i=indexOfChild){
indexOfChild=getLeftChildIndex(i);
if(indexOfChild !=length-1 && s[indexOfChild].compareTo(s[indexOfChild+1])<0){
indexOfChild++;
}
if(temp.compareTo(s[indexOfChild])<0){
s[i] = s[indexOfChild];
} else{
break;
}
}
s[i] = temp;
}
public static void swap(String s[],int x,int y){
String temp=s[x];
s[x]=s[y];
s[y]=temp;
}
}
The while loop isn't terminating when there is no swap between the parent and the child(i.e. the parent is greater than the child).
The value of i(just above main method) doesn't change when parent is greater. Simply taking the line i=max outside the if block [if(heap[i]
Also,is there any sequence to learn Algorithms?If so,kindly guide me.
Thank you.
Newbie code for printing a matrix:
import java.util.*;
import java.io.File;
public class Strings {
public static void main(String[] args){
Strings String1 = new Strings();
int alen =0 ,blen =0;
String a,b;
int [][] matrix = new int[alen+1][blen+1];
System.out.println("Enter String a: ");
Scanner usrip = new Scanner(System.in);
a = usrip.next();
System.out.println("Enter String b: ");
b = usrip.next();
System.out.println("Execute print method: ");
String1.printMatrix();
}//end of main
public void printMatrix(){
for(int i=0;i<alen+1;i++)
{
for(int j=0;i<blen+1;j++)
{
System.out.print(matrix[i][j]);
}
}
}//end of printMatrix
}// End class
Since alen , blen are declared in the class not in a mehtod I thought they were global varaibales. But looks like Its not what I think it is.
The error I get is alen cannot be resolved into a variable same for blen and matrix as well.
Same error when I try to access them like String1.alen as well.
the variables "alen" and "blen" you declared in main() are method-local, that means they are not accessible from printMatrix() method. Make them fields instead by writing:
private int alen;
private int blen;
just below the line public class Strings {
or pass them as arguments to printMatrix() method, as Christian suggested.
You cannot access alen, blen and matrix since they are not declared within the printMatrix() scope (you may want to read about scope in Java), here is one simple solution:
Pass the variables as arguments:
public void printMatrix(int alen, int blen, int[][] matrix){
...
}
and call in the main method:
String1.printMatrix(alen, blen, matrix);
Of course, that this is not necessary, you could just do:
public void printMatrix(int[][] matrix){
for(int i=0;i<matrix.length;i++)
{
for(int j=0;i<matrix[i].length;j++)
{
System.out.print(matrix[i][j]);
}
}
}
It is a bit ugly, but for a beginner, you can do the following:
public class Strings {
public int [][] matrix;
public static void main(String[] args){
Strings String1 = new Strings();
int alen =0 ,blen =0;
String a,b;
String1 = new int[alen+1][blen+1];
...
public void printMatrix(){
for(int i=0;i<matrix.length;i++)
{
for(int j=0;i<matrix[0].length;j++)
{
System.out.print(matrix[i][j]);
}
}
}//end of printMatrix
}// End class
this is one way among many:
public void printMatrix(int[][] matrix){
for(int i=0;i<matrix.length;i++) {
for(int j=0;i<matrix[i].length;j++) {
System.out.print(matrix[i][j]);
}
}
}//end of printMatrix`enter code here
call it like:
String1.printMatrix(matrix);
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);
}
}
}