The Artima Developer Community
Sponsored Link

Java Answers Forum
Creating arrays of objects

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
I S

Posts: 1
Nickname: sunshyn
Registered: Jul, 2010

Creating arrays of objects Posted: Jul 15, 2010 7:58 AM
Reply to this message Reply
Advertisement
Can anybody please help me run this program.
I want main() method to create 4 array objects after checking validity of quantity and price.
But its not working out.Please help.

import java.io.*;
import java.util.*;
 
 
public class Computer2{
 
String[] compName = new String[4];
String[] ramSize = new String[4];
int[] hdCapacity = new int[4];
String[] pMake = new String[4];
double[] pSpeed = new double[4];
int[] cQuan = new int[4];
long[] cPrice = new long[4];
int k=0;
 
  // constructor computer2
 
Computer2(String name[],String size[],int capacity[],String make[], double speed[], int quan[],long price[] )
{
compName =name;
ramSize = size;
hdCapacity = capacity;
pMake = make;
pSpeed = speed;
cQuan = quan;
cPrice = price;
}
       // to check validity of quantity and price
 
int check() {
int k=0;
 
if(cQuan[k]>0 && cPrice[k]>0) { return 0;}
 
try { if(cQuan[k]<=0); 
 
     }
catch (Exception e){
 
         System.out.println("\nSorry! No computers available of this configuration.");
          
         System.exit(0);}
 
try {if(cPrice[k]<=0);
    } 
catch (Exception ex){
 
       System.out.println("\nSorry! No computers of this configuration are available at this price.");
 
       System.exit(0);  }
 
return 0;
        
} //end check
 
 
void printDescription(){
 
System.out.println("\nComputer Name            : "+compName[k]);
System.out.println("\nRAM Size                 : "+ramSize[k]);
System.out.println("\nHard Disk Capacity       : "+hdCapacity[k]+" GB");
System.out.println("\nProcessor Make           : "+pMake[k]);
System.out.println("\nProcessor Speed          : "+pSpeed[k]+" GHz");
System.out.println("\nQuantity                 : "+cQuan[k]);
 
long tPrice = cPrice[k] * cQuan[k];
System.out.println("\nTotal Price of Computers : Rs."+tPrice+"/-");
 
System.out.println();
 
}
 
 
public static void main(String args[]) {
 
try {
 
    BufferedReader bk = new BufferedReader(new InputStreamReader(System.in));
 
      
    String[] name = new String[4];
    String[] size = new String[4];
    String[] make = new String[4];
 
    int[] capacity = new int[4];
    int[] quan = new int[4];
    
    double[] speed = new double[4];
    long[] price = new long[4];
      
   Computer2[] comp = new Computer2[4];   // Create an array of four objects 
     
    int k=0;  
      
 for(k=0;k<4;k++) {   
     
    System.out.println("\nPlease enter the name of the company : ");
    name[k] = bk.readLine();
    name[k].toUpperCase();
    System.out.println(); 
   
    System.out.println("\nPlease enter the size of the RAM : ");    
    size[k] = bk.readLine();
    System.out.println();
 
    System.out.println("\nPlease enter the capacity of hard disk : ");
    capacity[k] = Integer.parseInt(bk.readLine());
    System.out.println();
 
    System.out.println("\nPlease enter the make of the processor : ");
    make[k] = bk.readLine();
    System.out.println();
 
    System.out.println("\nPlease enter the speed of the processor :");    
    speed[k] = Double.parseDouble(bk.readLine());
    System.out.println();
 
    System.out.println("Please enter the number of computers : ");    
    quan[k] = Integer.parseInt(bk.readLine());
    System.out.println();
 
    System.out.println("Please enter the price : ");    
    price[k] = Integer.parseInt(bk.readLine());
    System.out.println("     --- XXXX ---  "); 
 
     
     comp[k].check();
                                // create objects if quantity and price are valid
 
 if((quan[k]>0) & (price[k]>0)) {
   
      System.out.println("  - " + comp[k]);
 
      comp[k] = new Computer2(String name[],String size[],int capacity[],String make[], double speed[], int quan[],long price[] );
      comp[k].printDescription(); }    
 
  }       
System.out.println();
 
bk.close(); 
 
}
// catch blocks
 
 
} // End Main
 
} // End Computer2
 

Topic: Who could solve this problem i wiil be really grateful Previous Topic   Next Topic Topic: adding servlet

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use