If you made an object from the class Paupers sending 5 and -4 to the constructor, then sending -6 and 2 to the method divers, what value would the method divers return? What would be the values of the instance identifiers?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
class Paupers
{
private myTrams:number, myWeekly:number;
constructor(digest:number, causes:number){
if(causes == 0) causes = 2;
this.myWeekly = digest % causes;
this.myTrams = digest * causes;
}
public divers(crucial:number, lockbox:number){
crucial = crucial - lockbox;
this. myTrams = this.myTrams + lockbox;
return myTrams;
}
}
The returned value is -18
. The instance identifier values are -18
and 1
.
Write a class named Belated that has two instance variables, a string named myHypos and a number named myDroller. Give the class a constructor method with a string parameter named kopeks, which provides an initial value to myHypos. Include a method named locusts within the class that would return the value of myDroller to a client.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
class Belated
{
private myHypos:string;
private myDroller:number;
constructor(kopeks:string)
{
this.myHypos = kopeks;
}
public locusts()
{
return myDroller;
}
}
Write a class named Curse that has two instance variables, a string named myMosses and a number named myMacula. Give the class a constructor method with a string parameter named thud, which provides an initial value to myMacula. Include a method named pronged within the class that would return true if MyMacula is greater than 0 and false otherwise.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
class Curse
{
private myMosses:string;
private myMacula:number;
constructor(thud:number)
{
this.myMacula = thud;
}
public pronged()
{
return myMacula > 0;
}
}
Write a class called Bobbies
that has one private instance variable, jo
with an initial value of “what?” and an empty constructor. Add a static method named merles
that accepts formal parameters wagging
and reaching
as numbers, finds their sum, stores the result in a local variable call verbs
then returns the value of verbs
. Outside the class, create a new Bobbies object named b
, then call the merles
method with the values 2 and 3, storing the result in a global variable called yo
.
1
2
3
4
5
6
7
8
9
10
11
12
13
class Bobbies {
private jo = "what?";
constructor() {}
static merles(wagging:number, reading:number) {
let verbs = wagging + reading;
return verbs;
}
}
let b = new Bobbies();
let yo = Bobbies.merles(2, 3);
Which expression has been evaluated correctly?
e = 50 / (2 / 2) % 2;
//e = 25c = 33 % 5 + 7 % 3;
//c = 4a = 2 + 2 * 3;
//a = 12b = 3 * 6 + 10 / 5 + 5;
//b = 10d = 50 / 2 / 2 * 2;
//d = 25B is correct. You should be able to evaluate all of these.
Consider the class below:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
class Dog {
private breed:string;
private name:string;
private weight:number;
constructor(/*code missing */) {
/* code missing */
}
static public sound():string {
return "bark!";
}
public speak():string {
return this.name + " says " + Dog.sound();
}
public eat(amount:number):void {
/* code missing */
}
}
/* Code missing */
Identify the instance variables.
breed, name, and weight
Complete the constructor code so that it sets all instance variables.
1
2
3
4
5
constructor(breed:string, name:string, weight:number) {
this.breed = breed;
this.name = name;
this.weight = weight;
}
Write code to create two new dogs of your choice, then make one of them speak. Predict the return output of your dog speaking.
1
2
3
let rover = new Dog("dachsund", "Rover",8.2);
let django = new Dog("mutt", "Django", 21.3);
django.speak() // output will be "Django says 'bark!'"
Explain the difference between the static method sound()
and the instance method speak()
The static method sound
is called in the format Dog.sound()
and returns the sound for any Dog. The instance method speak()
is called in the form d.speak()
for some specific Dog object d
, and makes the specific dog speak; that is why it has access to the this
keyword while static methods do not.
Implement the code for the eat()
method, which should increase dog’s weight by the amount it wait (we will not implement a poop()
method, its reverse.)