Question

In: Computer Science

Write a tester program to test the mobile class defined below. Create the class named with...

Write a tester program to test the mobile class defined below.

Create the class named with your id (for example: Id12345678) with the main method.

  1. Create two mobiles M1 and M2 using the first constructor.
  2. Print the characteristics of M1 and M2.
  3. Create one mobile M3 using the second constructor.
  4. Change the id and brand of the mobile M3. Print the id of M3.

Your answer should include a screenshot of the output. Otherwise, you will be marked zero for this question.

public class Mobile {

private int id;

private String brand;

public Mobile() {

    id = 0;

    brand = "";

}

public Mobile(int n, String name) {

    id = n;

    brand = name;

}

public void setBrand(String w) {

    brand = w;

}

public void setId(int w) {

    id = w;

}

Public int getId() {

Solutions

Expert Solution

class Mobile {

private int id;

private String brand;

public Mobile() { // Constructor 1 - Default Constructor

id = 0;

brand = "";

}

public Mobile(int n, String name) { // Constructor 2 - Parameterised Constructor

id = n;

brand = name;

}

public void setBrand(String w) {

brand = w;

}
public void printCharacterstics()
{
System.out.printf("Id:%d\n",id);
System.out.printf("Brand:"+brand,"\n");
System.out.printf("\n");
}
public void setId(int w) {

id = w;

}

public int getId()
{
return id;
}
public int changeId(int n){
id = n;
return 0;
}
public int changeBrand(String b){
brand = b;
return 0;
}


}
class Main
{
   public static void main(String[] args) {
   Mobile m1 = new Mobile(); // Create object m1 with constructor 1


   Mobile m2 = new Mobile(); // Create object m2 with constructor 1


   m1.setId(1100123456); // Set the id of m1


   m1.setBrand("Samsung"); // Set the Brand of m1


   m2.setId(1100654123); // Set the id of m2


   m2.setBrand("Apple"); // Set the Brand of m1

   System.out.println("Characterstics of M1:\n");


   m1.printCharacterstics(); // Call function printCharacterstics to print the characterstics

   System.out.println("\nCharacterstics of M2:\n");

   m2.printCharacterstics();

   Mobile m3 = new Mobile(1100789456,"Huwai"); // Create the object m3 with second constructor

   System.out.printf("\nCharacterstics of M3:\n");

   m3.printCharacterstics();

   m3.changeId(1234567890); // Change the id of m3

   m3.changeBrand("OnePlus"); // Change the brand of m3

   System.out.printf("\nId of M3(After Change):%d",m3.getId()); // Print the id of m3

  
   }
}

OUTPUT:


Related Solutions

Write a class called Name. A tester program is provided in Codecheck, but there is no...
Write a class called Name. A tester program is provided in Codecheck, but there is no starting code for the Name class. The constructor takes a String parameter representing a person's full name. A name can have multiple words, separated by single spaces. The only non-letter characters in a name will be spaces or -, but not ending with either of them. The class has the following method: • public String getName() Gets the name string. • public int consonants()...
Write a class called Name. A tester program is provided in Codecheck, but there is no...
Write a class called Name. A tester program is provided in Codecheck, but there is no starting code for the Name class. The constructor takes a String parameter representing a person's full name. A name can have multiple words, separated by single spaces. The only non-letter characters in a name will be spaces or -, but not ending with either of them. The class has the following methods. • public String getName() Gets the name string. • public int consonants()...
write program in java Create a class named PersonalDetails with the fields name and address. The...
write program in java Create a class named PersonalDetails with the fields name and address. The class should have a parameterized constructor and get method for each field.  Create a class named Student with the fields ID, PersonalDetails object, major and GPA. The class should have a parameterized constructor and get method for each field. Create an application/class named StudentApp that declare Student object. Prompts (GUI input) the user for student details including ID, name, address, major and GPA....
Task: Write a program that creates a class Apple and a tester to make sure the...
Task: Write a program that creates a class Apple and a tester to make sure the Apple class is crisp and delicious. Instructions: First create a class called Apple  The class Apple DOES NOT HAVE a main method  Some of the attributes of Apple are o Type: A string that describes the apple. It may only be of the following types:  Red Delicious  Golden Delicious  Gala  Granny Smith o Weight: A decimal value representing...
Write and test a user-defined class (requiring conditions). Write an application (client) program that uses an...
Write and test a user-defined class (requiring conditions). Write an application (client) program that uses an instance(s) of a user-defined class. The federal income tax that a person pays is a function of the person's taxable income. The following table contains formulas for computing a single person's tax. Bracket Taxable Income Tax Paid 1 $22,100 or less 15% 2 More than $22,100 but $53,500 or less $3,315 plus 28% of the taxable income over $22,100 3 More than $53,500 but...
Java program Create a public method named saveData for a class named Signal that will hold...
Java program Create a public method named saveData for a class named Signal that will hold digitized acceleration data. Signal has the following field declarations private     double timeStep;               // time between each data point     int numberOfPoints;          // number of data samples in array     double [] acceleration = new double [1000];          // acceleration data     double [] velocity = new double [1000];        // calculated velocity data     double [] displacement = new double [1000];        // calculated disp...
Java Programming Create a class named Problem1, and create a main method, the program does the...
Java Programming Create a class named Problem1, and create a main method, the program does the following: - Prompt the user to enter a String named str. - Prompt the user to enter a character named ch. - The program finds the index of the first occurrence of the character ch in str and print it in the format shown below. - If the character ch is found in more than one index in the String str, the program prints...
Write a java program that has a class named Octagon that extends the class Circ and...
Write a java program that has a class named Octagon that extends the class Circ and implements Comparable (compare the object's area) and Cloneable interfaces. Assume that all the 8 sides of the octagon are of equal size. Your class Octagon, therefore, must represent an octagon inscribed into a circle of a given radius (inherited from Circle) and not introduce any new class variables. Provide constructors for clas Octagon with no parameters and with 1 parameter radius. Create a method...
1. Create a class named Mobile 2. Add IEMICode, processor, make, model and color properties to...
1. Create a class named Mobile 2. Add IEMICode, processor, make, model and color properties to the Mobile class 3. Create a methods connectBlueTooth, sendMessage, changeColor, displayInfo in the class Mobile. 4. Write a python script that creates two instances of the class Mobile, changes their colors to Black and Pink and prints a message to the console to display the object attributes using the displayInfo method 5. Run the program and observe the message in Console
Create a file named StudentArrayList.java,within the file create a class named StudentArrayList. This class is meant...
Create a file named StudentArrayList.java,within the file create a class named StudentArrayList. This class is meant to mimic the ArrayList data structure. It will hold an ordered list of items. This list should have a variable size, meaning an arbitrary number of items may be added to the list. Most importantly this class should implement the interface SimpleArrayList provided. Feel free to add as many other functions and methods as needed to your class to accomplish this task. In other...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT