Having Difficulties With a Multi Thread Code... Help?
There is an issue with part of my program, I get two errors.. And I have no idea how to fix it. Here is what I have for the code:
Code Java:
class Customer{
private String name;
private int age;
public Customer(String n, int a)throws CreateException{
if(0<a<125){
Name=n;
Age=a;
throw new CreateException(“ Age limit is 0 to 125 ”);
}
}
public void print(){
System.out.println(" The Name:" +Name);
System.out.println(" The Age:" +Age);
}
}
Code Java:
public class CustomerDemo{
public static void main(String args[]){
Customer cust[]=new Customer[2];
for(int i=0;i<2;i++){
try{
c[0]=new Customer(“Ram”,60);
c[0].print();
}catch(CreateException e){
System.out.println(“ Complete ”);
System.out.println(e.getMessage());
}
}
}
}
Re: Having Difficulties With a Multi Thread Code... Help?
Hi... I think you have to work hard on your basics..
1) if(0<a<125) condition is wrong. It could be if((0<a)&&(a<125))
2) Your declared variables are
private String name;
private int age;
And you are using
Name=n;
Age=a;
This is again basic, declared name of variable are different then assigned one.
3) In customer demo class
c[0]=new Customer(“Ram”,60);
c[0].print();
Where is the array of c, i think it should be cust.
---------------------------------------------------
Code Java:
package com.java.main;
class Customer {
private String name;
private int age;
public Customer(String n, int a) throws Exception {
if ((0 < a) && (a < 125)) {
name = n;
age = a;
throw new Exception("Age limit is 0 to 125");
}
}
public void print() {
System.out.println(" The Name:" + name);
System.out.println(" The Age:" + age);
}
}
----------------------------------------------------
Code Java:
package com.java.main;
public class CustomerDemo {
public static void main(String args[]) {
Customer cust[] = new Customer[2];
for (int i = 0; i < 2; i++) {
try {
cust[0] = new Customer("Ram", 60);
cust[0].print();
} catch (Exception e) {
System.out.println("Complete");
System.out.println(e.getMessage());
}
}
}
}