Question

how would i write code in java/netbeans for this: You will be creating the driver class...

how would i write code in java/netbeans for this:

You will be creating the driver class for this homework (PA05_TVs.java) and the object/element class, TV.java. Your TV class should include only a channel value for instance data (3-99) and the following methods: constructor [requires an initial channel value], getter, setter, randomChannel() [does not return the new channel value], and toString().

The driver class must complete the following:

Instantiate a TV object and set the initial channel to 97.

Instantiate a second TV object and set the initial channel to 63.

Print both the TV objects using a heading for each (ex. TV1: ).

Print a spacer between sets of output.

Set the channel on the first TV to channel 4.

Use the randomChannel method to change the channel on the second TV.

Print both the TV objects using a heading for each.

Be sure to include a message to the screen that explains the purpose of the program to the user as part of a clear statement.

Your output should look similar to this:

The following program creates 2 TV objects and uses the methods to change the channel, get the channel, and print out the TV's information.

TV1: The TV is currently set to channel 97

TV2: The TV is currently set to channel 63

--------------------------------------------------------

TV1: The TV is currently set to channel 4

TV2: The TV is currently set to channel 22

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Program:

import java.util.Random;

public class TV {

private int channel; //channel variable

TV(int ch)

{

channel=ch; //assigning value to channel variable in the constructor

}

@Override

public String toString() //overriding channel value to return channel value

{

return String.format(channel+" ");

}

public int getChannel() {

return channel;

}

public void setChannel(int channel) {

this.channel = channel;

}

public void randomChannel()

{

Random rd=new Random();

int h=99;

int l=3;

channel=rd.nextInt(h-l)+l;

}

}

Driver Clas:

public class TvTest {

public static void main(String[] args) {

TV TV1= new TV(97); // creating TV1 object and setting channel value to 4

TV TV2=new TV(63); // creating TV2 object and setting channel value to 63

System.out.println("The following program creates 2 TV objects and uses the methods to change the channel, get the channel, and print out the TV's information.");

System.out.println("TV1: The TV is currently set to channel "+TV1.getChannel());

System.out.println("TV2: The TV is currently set to channel "+TV2.getChannel());

TV1.setChannel(4); // setting channel to 4 in TV1 object

TV2.randomChannel(); //setting channel by calling random function in TV2

System.out.println("-----------------------------------------------------------");

System.out.println("TV1: The TV is currently set to channel "+TV1.getChannel());

System.out.println("TV2: The TV is currently set to channel "+TV2.getChannel());

}

}

output:

Add a comment
Know the answer?
Add Answer to:
how would i write code in java/netbeans for this: You will be creating the driver class...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • You will be creating a driver class and 5 class files for this assignment. The classes...

    You will be creating a driver class and 5 class files for this assignment. The classes are Shape2D (the parent class), Circle, Triangle, and Rectangle (all children of Shape2D) and Square (child of Rectangle). Be sure to include a message to the user explaining the purpose of the program before any other printing to the screen. Within the driver class, please complete the following tasks: Instantiate a Circle object with 1 side and a radius of 4; print it Update...

  • Start a new project in NetBeans that defines a class Person with the following: Instance variables...

    Start a new project in NetBeans that defines a class Person with the following: Instance variables firstName (type String), middleInitial (type char) and lastName (type String) (1) A no-parameter constructor with a call to the this constructor; and (2) a constructor with String, char, String parameters (assign the parameters directly to the instance variables in this constructor--see info regarding the elimination of set methods below) Accessor (get) methods for all three instance variables (no set methods are required which makes...

  • Write a Java class that implements the concept of Coins, assuming the following attributes (variables): number...

    Write a Java class that implements the concept of Coins, assuming the following attributes (variables): number of quarters, number of dimes, number of nickels, and number of pennies. Include two constructors (non-argument constructor that assigns 1 to each coin, and another constructor that takes necessary arguments to create an object), needed getter and setter methods, method toString (to print coins objects in a meaningful way), and method equals (to compare two coins objects based on number of coins). Also include...

  • Help please 1) Define a Java class for defining the activity of a TV set. The...

    Help please 1) Define a Java class for defining the activity of a TV set. The class contains A private int data field named channel that specifies what channel is currently set for the tuner (channels range from 1 to 120, default is 1) a. b. A private int data field named volumeLevel that specifies the volume level of the TV (range is 1 to 7; default is 1) A private boolean data field named on that determines whether the...

  • Please help me with this code. Thank you Implement the following Java class: Vehicle Class should...

    Please help me with this code. Thank you Implement the following Java class: Vehicle Class should contain next instance variables: Integer numberOfWheels; Double engineCapacity; Boolean isElectric, String manufacturer; Array of integers productionYears; Supply your class with: Default constructor (which sets all variables to their respective default values) Constructor which accepts all the variables All the appropriate getters and setters (you may skip comments for this methods. Also, make sure that for engineCapacity setter method you check first if the vehicle...

  • In this assignment you will create two Java programs: The first program will be a class...

    In this assignment you will create two Java programs: The first program will be a class that holds information (variables) about an object of your choice and provides the relevant behavior (methods) The second program will be the main program which will instantiate several objects using the class above, and will test all the functions (methods) of the class above. You cannot use any of the home assignments as your submitted assignment. Your programs must meet the following qualitative and...

  • I Need Help with this using Java Programming : Class name fname lname Class variable total...

    I Need Help with this using Java Programming : Class name fname lname Class variable total Number Constructor (no arguments) Constructor (takes two arguments, fname and lname) One getter method to return the fname and lname One setter method for fname and lname Inside Main: Create three objects with the following names Jill Doe John James Jack Smith When creating the first object (Should not be an anonymous object) use the argument-less constructor Then use the setter method to assign...

  • Creating a Programmer-Defined Class in Java Summary In this lab, you will create a programmer-defined class...

    Creating a Programmer-Defined Class in Java Summary In this lab, you will create a programmer-defined class and then use it in a Java program. The program should create two Rectangle objects and find their area and perimeter. Instructions Make sure the class file named Rectangle.java is open. In the Rectangle class, create two private attributes named lengthand width. Both length and width should be data type double. Write public set methods to set the values for length and width. Write...

  • // Client application class and service class Create a NetBeans project named StudentClient following the instructions...

    // Client application class and service class Create a NetBeans project named StudentClient following the instructions provided in the Starting a NetBeans Project instructions in the Programming Exercises/Projects Menu on Blackboard. Add a class named Student to the StudentClient project following the instructions provided in the Starting a NetBeans Project instructions in the Programming Exercises/Projects Menu on Blackboard. After you have created your NetBeans Project your application class StudentC lient should contain the following executable code: package studentclient; public class...

  • In java code: Write a Temperature class that has two private instance variables: • degrees: a...

    In java code: Write a Temperature class that has two private instance variables: • degrees: a double that holds the temperature value • scale: a character either ‘C’ for Celsius or ‘F’ for Fahrenheit (either in uppercase or lowercase) The class should have (1) four constructor methods: one for each instance variable (assume zero degrees if no value is specified and assume Celsius if no scale is specified), one with two parameters for the two instance variables, and a no-argument...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT