Receiving 5,9,7 as output.. Where as the expected should be only 9..Below is the code:
public class GreatestNoInArray {
public static void main(String[] args) {
int a[]= new int[] {1,2,5,9,7};
int big=a[0];
for (int i=1; i<a.length; i++){
if (big<a[i])
big=a[i];
System.out.println(a[i]);
}
}
}
Please help
for expected answer you need to print big (not a[i]) out side of loop
public class GreatestNoInArray {
public static void main(String[] args) {
int a[]= new int[] {1,2,5,9,7};
int big=a[0];
for (int i=1; i<a.length; i++){
if (big<a[i])
big=a[i];
}
System.out.println(big);
}
}
code should be like this:
public class GreatestNoInArray {
public static void main(String[] args) {
int a[] = new int[] {1,2,5,9,7};
int big = a[0];
for (int i=1; i<a.length; i++){
if (big < a[i])
big = a[i];
//System.out.println(a[i]);
}
System.out.println("Big: " + big);
}
}
This works!
Related
link to the problem for reference: https://www.geeksforgeeks.org/nutanix-interview-question-placement-2019-2020/
Array out-of-bounds exception at line prp[b]=s1.nextInt();
Stuck on a couple of questions with same error.
Appreciate any help.TIA
Code:
package pkg161219;
import java.util.*;
public class xD{
static Scanner s1=new Scanner(System.in);
public static void main(String[] args)
{
int x=s1.nextInt();
for(int i=0;i<x+1;i++)
{
int[] y=new int[3];
for(int a=0;a<3;a++)
{
y[i]=s1.nextInt();
}
int[] prp=new int[y[0]];
int[] prr=new int[y[1]];
int[] prs=new int[y[2]];
for(int b=0;b<y[0];b++)
{
System.out.println("Enter prices of specs of Processor");
prp[b]=s1.nextInt();
}
for(int c=0;c<y[1];c++)
{
System.out.println("Enter prices of specs of RAM");
prr[c]=s1.nextInt();
}
for(int d=0;d<y[2];d++)
{
System.out.println("Enter prices of specs of SSD");
prs[d]=s1.nextInt();
}
for(int q=0;q<y[0];q++)
{
for(int w=0;w<y[1];w++)
{
for(int e=0;e<y[2];e++)
{
if(prr[e]>=prs[w]&&prr[e]>=prp[q])
{
System.out.println(prp[q]*prr[w]+prr[w]*prs[e]);
}
}
}
}
}
}
}
for(int a = 0; a < 3; a++) {
y[i] = s1.nextInt();
}
The above is wrong. It should be y[a]
Using two for loops:
import java.util.*;
public class InsertionSort{
public static int[] doInsertionSort(int[]a){
int j=a.length;
for(int k=1;k<j;k++){
for(int i=k;i>0;i--){
if(a[i]<a[i-1]){
int temp=a[i];
a[i]=a[i-1];
a[i-1]=temp;
}
//for printing the elements while sorting..
for(int o:a)
System.out.print(o+" ");
System.out.println("");
}
}
return a;
}
public static void main(String[] args) {
int[]arr={99,77,55,33,11,88,66};
int []arra= doInsertionSort(arr);
for(int i:arra)
System.out.print(i+" ");
}
}
Using a for and while loop:
import java.util.*;
public class InsertionSortAgain{
public static void main(String[] args) {
int[]arr={99,77,55,33,11,88,66};
int n=arr.length,j;
for(int i=1;i<n;i++){
j=i-1;
int key=arr[j+1];
while(j>=0 && arr[j]>key){
arr[j+1]=arr[j];
j=j-1;
//for printing elements while sorting.....
for(int k :arr)
System.out.print(k+" ");
System.out.println(" ");
}
arr[j+1]=key;
}
for(int k :arr)
System.out.print(k+" ");
}
}
I have tried both and both do fine, but I couldn't get which is optimized.
And whether insertion sort can be implemented like this or not?
I am trying to run my project, but the main class isn't being found. Whats wrong? Here's my code.
public class ArrayPrinter {
public static void printArray(int[] arr) {
int size = arr.length;
System.out.print("[");
for(int i=0;i< size; i++){
System.out.print(arr[i]);
if(i<size-1){
System.out.print(",");
}
}
System.out.println("]");
}
}
You need a main method, not class. See below:
public class ArrayPrinter {
public static void main(String[] args){
//Sample use
int[] arr = new int[2];
arr[0] = 4;
arr[1] = 2;
printArray(arr);
}
public static void printArray(int[] arr) {
int size = arr.length;
System.out.print("[");
for(int i = 0;i < size; i++){
System.out.print(arr[i]);
if (i < size-1){
System.out.print(",");
}
}
System.out.println("]");
}
}
The signature of the main function always looks the same:
public class ArrayPrinter {
public static void main(String[] args) {
// put code here
}
public static void printArray(int[] arr) {
int size = arr.length;
System.out.print("[");
for(int i=0;i< size; i++){
System.out.print(arr[i]);
if(i<size-1){
System.out.print(",");
}
}
System.out.println("]");
}
}
You need to have a main method in order to run a Java application.
The signature of the main method is
public static void main (String[] args)
More info can be found
here
I don't know what's wrong with this code:
class A{
private int i,j;
public void get(int i, int j)
{
this.i=i;
this.j=j;
}
public void show()
{
System.out.println(i + " " + j);
}
}
public class App {
public static void main(String[] args) {
A[] c = new A[11];
for(int i=0; i<10; i++)
{
c[i].get(i, i);
}
for(int j=0; j<10; j++)
{
c[j].show();
}
}
}
can anyone please tell me the right way to call the get() method??
thanks in advance...
for(int i=0; i<10; i++)
{
c[i] = new A();
c[i].get(i, i);
}
You had an empty array. You need to fill that array with elements
The problem is in method "calculusRank". If I delete that method and related parts to it, aplication will work, if I create a different notepad with same code(method and main code related to that method) it's work.
Also I wanna say I'm a beginner and this is my first "relative" large application.
This is the code with problem that doesn't compile:
import java.util.Random;
import java.lang.Math;
public class MultilevelSystem1 {
static String[][] createMembers (int a) {
Random nr = new Random ();
String[][] membersName = new String [a][2];
for(int j=0; j<2;j++)
for(int i=0; i<a; i++) {
if(j==0||i==0) membersName[i][j]="Nume"+(i+1);
else membersName[i][j]="Nume"+(nr.nextInt(i)+1);
}
return membersName;
}
static String[][] createIncomes (String[][] a,int b){
Random nr = new Random ();
String[][] incomes = new String [b][2];
int j=0;
for(int i=0;i<a.length && j<b;i++)
if(nr.nextInt(2)==1){ incomes[j][0]= a[i][0];
incomes[j][1]=Integer.toString((nr.nextInt(10)+1)*50);
j++;}
return incomes;
}
static class Members extends MultilevelSystem1 {
double capital;
String name;
int rank;
String superior;
int ID;
int calculateRank (int r, Members[] membersArray) {
if(this.superior!=null)
for(int i=0;i<membersArray.length;i++)
if((this.superior).equals(membersArray[i].name)){r=membersArray[i].calculateRank(r,membersArray);
r++;
break;}
return r;
}
static int[] calculate (int[] a, int[] b) {
int[] c= new int [a.length];
for (int i=0; i<a.length; i++)
c[i]=a[i]+b[i];
return c;
}
}
public static void main(String[] args) {
final int n = 30;
final int m = 10;
int[] a={1,2,3,4,5};
int[] b={1,2,3,4,5};
int[] c= calculate(a,b);
for(int i=0;i<c.length;i++)
System.out.print(c[i]+" ");
String[][] entryDataMembers = createMembers(n);
String[][] entryDataIncomes = createIncomes(entryDataMembers,m);
Members[] membersArray = new Members[n];
for (int i=0; i<n; i++) {
membersArray[i]=new Members();
membersArray[i].name = entryDataMembers[i][0];
if(i!=0) membersArray[i].superior= entryDataMembers[i][1];
else membersArray[0].superior= null;
}
for (int i=0; i<n; i++)
membersArray[i].rank = membersArray[i].calculateRank(1,membersArray);
}
}
And this work:
public class test {
static int[] calculate(int[] a, int[] b) {
int[] c= new int [a.length];
for (int i=0; i<a.length; i++)
c[i]=a[i]+b[i];
return c;
}
public static void main (String[]args) {
int[] a={1,2,3,4,5};
int[] b={1,2,3,4,5};
int[] c= calculate(a,b);
for(int i=0;i<c.length;i++)
System.out.print(c[i]+" ");
}
}
int[] c = Members.calculate(a,b);
In you code there is compilation issue for calculate(a,b) method, this method present inside Members class. calculate(a,b) is static method so you can call this method using Members class.
The method which compiler couldn't find is "calculate".
Use
int[] c= Members.calculate(a,b);
to access static method of member class. (Around 62 line of code inside main method)