Question

In: Computer Science

//Question1: What is the name of the following lines in the data type class? private int...


//Question1: What is the name of the following lines in the data type class? private int intVariable;
private float floatVariable;
private String stringVariable;

//Question2: What is the name of the following lines in the data type class? public DataTypeClass_Smith()
{
intVariable = 0; floatVariable = 0.0; stringVariable = “aString”;
}

//Question3: What is the name of the following lines in the data type class? public DataTypeClass_Smith( int intVar, float floatVar, String stringVar)
{
intVariable = intVar; floatVariable = floatVar; stringVariable = stringVar;
}

//Question4: What is the name of the following method in the data type class? public void setIntVariable(int intVar)
{
intVariable = intVar; }


//Question5: What is the name of the following method in the data type class? public float getFloatVariable(0
{
return floatVariable;


//Question6: What is the purpose of the following method in the data type class? public String toString()
{
String str = “My name: James Smith\n” + “The output is: \n” +
“Integer number: “ + intVariable + “\n” + “Decimal number: “ + floatVariable + “\n” + “String is: “ + stringVariable + “\n”;
return str;
}

Solutions

Expert Solution

Question1 answer:

intVariable,floatVariable,stringVariables are instance variables of class DataTypeClass_Smith.

They hold the respective data(integer,float,string)for the class DataTypeClass_Smith.

Question 2 answer:

public DataTypeClass_Smith()

{

intVariable = 0; floatVariable = 0.0; stringVariable = “aString”;

}

DataTypeClass_Smith() is the default constructor of the class DataTypeClass_Smith and it initialise the instance variables with default values. When the object of type DataTypeClass_Smith is created with no parameters,then default constructor will be excuted and all the instance variables will be initialised with respective default values.

Question 3 answer:

public DataTypeClass_Smith( int intVar, float floatVar, String stringVar)

{

intVariable = intVar; floatVariable = floatVar; stringVariable = stringVar;

}

It is the parametrised constructor of the class DataTypeClass_Smith and it will initialise  all the instance variables with values present in the constructor.

Ex: DataTypeClass_Smith obj = new DataTypeClass_Smith(1,2.5,”John”);

parametrised constructor is called and values will be set to respective instance variables.

Question 4 answer:

It is a setter method for intVariable and it will set the value to the instance variable intVariable.

Ex: obj.setIntVariable(45);

Question 5 answer:

It is a getter method for floatVariable and it will return value of floatVariable when it is called.

Question 6 answer:

This is a toString() method for the class DataTypeClass_Smith.

It will print or return string format or readable format of the object which had all the instance variables with values


Related Solutions

Java Implement a class named “Fraction” with the following properties: numerator: int type, private denominator: int...
Java Implement a class named “Fraction” with the following properties: numerator: int type, private denominator: int type, private and the following methods: one default constructor which will create a fraction of 1/1. one constructor that takes two parameters which will set the values of numerator and denominator to the specified parameters. int getNum() : retrieves the value of numerator int getDenom(): retrieves the value of the denominator Fraction add(Fraction frac): adds with another Fraction number and returns the result in...
7.3 (The Account class) Design a class named Account that contains: ■ A private int data...
7.3 (The Account class) Design a class named Account that contains: ■ A private int data field named id for the account. ■ A private float data field named balance for the account. ■ A private float data field named annualInterestRate that stores the current interest rate. ■ A constructor that creates an account with the specified id (default 0), initial balance (default 100), and annual interest rate (default 0). ■ The accessor and mutator methods for id, balance, and...
Consider the following class: class Person {         String name;         int age;        ...
Consider the following class: class Person {         String name;         int age;         Person(String name, int age){                this.name = name;                this.age = age;         } } Write a java program with two classes “Teacher” and “Student” that inherit the above class “Person”. Each class has three components: extra variable, constructor, and a method to print the student or the teacher info. The output may look like the following (Hint: you may need to use “super”...
Design a class named Account that contains: A private int data field named id for the...
Design a class named Account that contains: A private int data field named id for the account. A private double data field named balance for the account. A private double data field named annualInterestRate that stores the current interest rate. A no-arg constructor that creates a default account with id 0, balance 0, and annualInterestRate 0. The accessor and mutator methods for id, balance, and annualInterestRate. A method named getMonthlyInterestRate() that returns the monthly interest rate. A method named withdraw(amount)...
In Java, Here is a basic Name class. class Name { private String first; private String...
In Java, Here is a basic Name class. class Name { private String first; private String last; public Name(String first, String last) { this.first = first; this.last = last; } public boolean equals(Name other) { return this.first.equals(other.first) && this.last.equals(other.last); } } Assume we have a program (in another file) that uses this class. As part of the program, we need to write a method with the following header: public static boolean exists(Name[] names, int numNames, Name name) The goal of...
JAVA -The next three questions use the following class: class Identification { private int idNum;   ...
JAVA -The next three questions use the following class: class Identification { private int idNum;    public Identification() { this(0); }    public Identification(int startingIdNum) { idNum = startingIdNum; }    public int getIdNum() { return idNum; }    public void setIdNum(int idNum) { this.idNum = idNum; } } Here is one program using the above class: public class Main {    public static void main(String[] args) {        Identification i1 = new Identification();        Identification i2 =...
Create a class named Horse that contains the following data fields: name - of type String...
Create a class named Horse that contains the following data fields: name - of type String color - of type String birthYear - of type int Include get and set methods for these fields. Next, create a subclass named RaceHorse, which contains an additional field, races (of type int), that holds the number of races in which the horse has competed and additional methods to get and set the new field. ------------------------------------ DemoHorses.java public class DemoHorses {     public static void...
class A { public: //constructors // other members private: int a; int b; }; Give declatations...
class A { public: //constructors // other members private: int a; int b; }; Give declatations of operator functions for each of the following ways to overload operator + You must state where the declatation goes, whether within the class in the public or private section or outside the class. The operator + may be overloaded. a) as friend function b) as member function c) as non-friend, non-member function
public class SinglyLikedList {    private class Node{        public int item;        public...
public class SinglyLikedList {    private class Node{        public int item;        public Node next;        public Node(int item, Node next) {            this.item = item;            this.next = next;        }    }       private Node first;    public void addFirst(int a) {        first = new Node(a, first);    } } 1. Write the method add(int item, int position), which takes an item and a position, and...
Write a class called Person that has two private data members - the person's name and...
Write a class called Person that has two private data members - the person's name and age. It should have an init method that takes two values and uses them to initialize the data members. It should have a get_age method. Write a separate function (not part of the Person class) called std_dev that takes as a parameter a list of Person objects and returns the standard deviation of all their ages (the population standard deviation that uses a denominator...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT