Jump to content

JAVA ADT programming

- - - - -

  • Please log in to reply
2 replies to this topic

#1
Zeeyo

Zeeyo

    Newbie

  • Members
  • Pip
  • 2 posts
I can't do this:
i) Find and display the total capacity of all labs
ii) Find and display the lab with the most number of computers
iii) list the lab number, capacity and computers available in each lab

here are my coding

ADT coding:

//The objectives of this coding is to set & get data of lab number, lab capacity and

//lab computer

public class laboratory

{

private int labnum;//lab number

private int labcap;//lab capacity

private int labcomp;//numbers of computers available


public laboratory()//not appropriate for the question

{

labnum = 0;

labcap = 0;

labcomp = 0;

}


public void setlabnum(int N)

{

labnum = N;

}

public void setlabcap(int C)

{

labcap = C;

}

public void setlabcomp(int K)

{

labcomp = K;

}

public int getlabnum()

{

return labnum;

}

public int getlabcap()

{

return labcap;

}

public int getlabcomp()

{

return labcomp;

}

public String toString()

{

return("***Laboratory Data*** \n" + "Laboratory Number : " + labnum + "\nLabatory Capacity : " + labcap + "\nComputer Available : " + labcomp );

}

}

Test coding.


//

//to test class laboratory



import javax.swing.*;


public class Testlaboratory

{

public static void main (String[]args)

{

laboratory[] lab = new laboratory[10];


String str;

int labnum, labcap, labcomp;

int sum = 0;


for (int i = 0; i < 10; i++)//a) input 10 data of lab into an array

{

lab[i] = new laboratory();


str = JOptionPane.showInputDialog("Enter laboratory number : ");

labnum = Integer.parseInt (str);

str = JOptionPane.showInputDialog("Enter laboratory capacity : ");

labcap = Integer.parseInt (str);

str = JOptionPane.showInputDialog("Enter the number of computers available : ");

labcomp = Integer.parseInt (str);


lab[i].setlabnum (labnum);

lab[i].setlabcap (labcap);

lab[i].setlabcomp (labcomp);

}


for (int i = 0; i < 10; i++)//b) find & display the total capacity for all labs

{

sum += lab[i].getlabcap();

}



for (int i = 0; i < 10; i++)//c)

{

double max = lab[i].getlabcomp();

int b=0;

if (lab[i].getlabcomp() > max)

{

max = lab[i].getlabcomp();

b=i;

}

System.out.println(lab[b].toString());

}

System.out.println("The total capacity is : " +sum);

System.out.println("The lab with the most computer is : ");


}

}


can u guys help me correct my work
thanks :)

#2
fayyazlodhi

fayyazlodhi

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 403 posts

Zeeyo said:

laboratory[] lab = new laboratory[10];

for (int i = 0; i < 10; i++)//a) input 10 data of lab into an array
{
lab[i] = new laboratory();
}

I didn't have a java system at hand with me to verify. But i think above mentioned code is your problem. I believe when you did the first

laboratory[] lab = new laboratory[10];

You have already created an array of objects. So doing a for loop and again creating objects won't make sense though it might compile.

This is only done when you are creating an array of references which are latter initialized to objects. However, when you use new with laboratory class type, it should already create an array of objects and not an array of references.

To be sure, just add a print in your constructor and re run your program. If print only comes when it goes into for loop, then i might be mistaken, but if it does that twice, then you know what to do.

Other than that is there a specific error you see? Or just your results don't tele?

#3
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Java
Actually
laboratory[] lab = [B][COLOR="#000080"]new[/COLOR][/B] laboratory[10];
Only creates the array, not the elements in it. So that part of Zeeyo's code is correct.

What seems wrong to me is the following:
[B][COLOR="#000080"]for[/COLOR][/B] ([B][COLOR="#000080"]int[/COLOR][/B] i = 0; i < 10; i++) { 

    [B][COLOR="#000080"]do[/COLOR][/B]uble max = lab[i].getlabcomp(); 

    [B][COLOR="#000080"]int[/COLOR][/B] b=0; 

    [B][COLOR="#000080"]if[/COLOR][/B] (lab[i].getlabcomp() > max)  { 

        max = lab[i].getlabcomp(); 

        b=i; 

    } 

    System.out.println(lab[b].toString()); 

}
I think you want to declare max and b outside of the loop, because every time you loop now, you reinitiliaze both.
For the first value of your max variable, you can use Double.min_value;
[B][COLOR="#000080"]do[/COLOR][/B]uble max = Double.min_value;

[B][COLOR="#000080"]int[/COLOR][/B] b=0; 

[B][COLOR="#000080"]for[/COLOR][/B] ([B][COLOR="#000080"]int[/COLOR][/B] i = 0; i < 10; i++) {    

    [B][COLOR="#000080"]if[/COLOR][/B] (lab[i].getlabcomp() > max)  { 

        max = lab[i].getlabcomp(); 

        b=i; 

    } 

    System.out.println(lab[b].toString()); 

}

Also note your last println is "empty"ish
System.out.println([B][COLOR="#008000"]"The lab with the most computer is : "[/COLOR][/B]);

Edited by wim DC, 10 June 2011 - 10:10 AM.





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users