0% found this document useful (0 votes)
18 views6 pages

203 - Part 2

Uploaded by

ahmedrssayeed821
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views6 pages

203 - Part 2

Uploaded by

ahmedrssayeed821
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

203- part 2

1- a. Dorine a class named "Electronics". Please add the followings inside the class:
i. Declare 3 instance variables: name, color, battery Percentage.
ii. Create a parameterized constructor with 3 parameters to initialize the instance
variables.
iii. Please add the following methods:
a) public String getName()
- Inside the method, return the name name of the Electronics.
● b). public void ruining(int time)
- Inside the method, decrease the battery Percentage by time amount. Here
batteryPercentage is the attribute/instance variable and time is the parameter of this
method.
● c). public void showCharging()
● - Inside the method, show "Charging is needed." if the batteryPercentage falls below 20%,
else show "No charging is needed."

1.b. Define a class named "Cellphone" and declare the main method inside the class.
Inside the main method, please do the following:
i. Create an object with "Electronics" class with name as "iPhone", color as "Black" and
batteryPercentage as "45%". Store the reference of the object to myPhone variable.
ii. Call the running( method using myPhone variable and pass the last 2 digits of your id as the
value of the time parameter.
iii. Call the showCharging() method using myPhone variable and show the output.

3.a. Write short notes on any two of the following:


i. Java Life Cycle
ii. JRE and JDK
iii. Data Types and Casting

3.b. Write a Java program that will take 3 integers as user input and print the sum of the
maximum and minimum number.

3.c. Identify the errors in the code below and fix the errors. You are not allowed to delete any
line of code. You can only add new line or cdit existing line.
package mid;
public class Test {
public static void main(String[] args) {
int num1, int num2;
num1 = 9.5;
System.out.println("%f", num1);
if(num1 % 2 != 0)
System.out.println(num2);
}
}
Or

package mid;
public class Patient {
public String name, gender;
private int age;

public Patient(String n, int a, String g) {


this.name = n;
this.age = a;
this.gender = g;
}
public int getAge() {
return age;
}
public void display() {
System.out.print(name + "=" + age + ":" + gender);
}
}

package mid.error;

public class TestError {


public static void main(String[] a) {
Patient pat = new Patient("Abir", 20);
System.out.println(pat.name);
System.out.println(pat.age);
System.out.println(pat.gender);
}
}

b. What will be output of the code below? Explain your steps in details.

package mid;
public class TestOutput {
public static void main(String[] ares) {
int[] array1 = new int[]{1, 3, 5, 7, 9};
int[] array2 = new int[]{2, 3, 6, 7, 5, 9};
int len = array1.length;
if (array2.length < len)
len = array2.length;
int result = 0;
for (int i = 0; i < len; i++) {
if (array1[i] > array2[i])
result += array1[i];
else
result += array2[i];
}
System.out.println("Result: " + result);
}
}
Or

public class Patient {


String name, gender;
private int age;

public Patient(String n, int a, String g) {


this.name = n;
this.age = a;
this.gender = g;
}
public void grow(int year) {
age += year;
}

public void visitDoctor(String symptom) {


display();
System.out.println("Visiting a doctor with following symptom:");
System.out.println(symptom);
}
public void display() {
System.out.println(name + ":" + age + ";" + gender);
}
}

⸻⸻⸻⸻⸻⸻⸻⸻—

public class TestPatient {

public static void main(String[] args) {


Patient pat = new Patient("Hasin", 22, "M");

pat.display();

fun(pat);

pat.visitDoctor("Fever and Cough");


}

public static void fun(Patient p) {


p.grow(3);
}
}

Patient p = new Patient("Abrar", 19, "M");


p.grow(1);
}
}

You might also like