Program Assignment

Fix the Errors

public class Constructor_Demo
{
int no1; 
float no2; 
public demo()
{
System.out.println("Hi"); 
}
public demo(int no1, float no2)
{
this.no1 = no1; 
this.no2 = no2; 
}
public static void main(String[] args)
{
Constructor_Demo cd1 = new Constructor_Demo(); 
Constructor_Demo cd2 = new Constructor_Demo(10,2.5);
}
}

Solution:

public class Constructor_Demo
{
int no1; 
float no2; 
 Constructor_Demo()
{
System.out.println("Hi"); 
}
 Constructor_Demo(int no1, float no2)
{
this.no1 = no1; 
this.no2 = no2; 
}
public static void main(String[] args)
{
Constructor_Demo cd1 = new Constructor_Demo(); 
Constructor_Demo cd2 = new Constructor_Demo(10,2.5f);
	cd2.print();
}
	public void print()
	{
		System.out.println(" no1 is "+ no1);
		System.out.println(" no2 is "+ no2);

	}
}

Output:

javac Constructor_Demo.java
java Constructor_Demo
Hi
 no1 is 10
 no2 is 2.5

Leave a Reply

Create your own website, free on WordPress.com.Create your own website.
Start free