Question

The attached Conversions.java source code file contains various incomplete declarations. Download the file and complete the...

The attached Conversions.java source code file contains various incomplete declarations. Download the file and complete the assignment statements so the code builds and runs successfully. Do not remove any code or add any new lines. Attach and submit your modified file.

public class Conversions {
public static void main(String[] args) {
  
double d = 13.57;
  
// 1. cast/convert the above variable to a float variable named f
float f = ;
  
// cast/convert the variable f, above, to a long variable named l (lower-case L)
long l = ;
  
// cast/convert the variable l, above, to an short variable named s
short s = ;
  
// encode an expression which uses the current value of the variable
// s to restore the original value of 13.57 to the variable f
f = ;
System.out.println("f is now " + f);
  
// encode an output statement which uses a ternary conditional operator
// to compare the variables s and f and prints "f > s" if f is greater
// than s, or "s > f" if s is greater than f
System.out.println(ternary operator goes here);
  
// declare and instantiate a SunFlower variable named sf
SunFlower sf = ;
  
// declare a Flower reference variable and assign the variable
// sf as its initial value
Flower flow = ;
  
// declare a Flower reference variable and instantiate it as a BlackEyedSusan
Flower flow2 = ;
// Declare a BlackEyedSusan reference variable and
// assign the variable flow2 as its initial value
BlackEyedSusan bes = ;
}
}

class Flower { }

class SunFlower extends Flower { }

class BlackEyedSusan extends SunFlower { }

Homework Answers

Answer #1

Modified lines are marked as bold in below code:

public class Conversions {
   @SuppressWarnings("unused")
   public static void main(String[] args) {
      
       double d = 13.57;
      
       // 1. cast/convert the above variable to a float variable named f
       float f =(float)d ; //13.57 in f
       // cast/convert the variable f, above, to a long variable named l (lower-case L)
       long l = (long)f; //13 in l
       // cast/convert the variable l, above, to an short variable named s
       short s = (short)l; //13 in s
       // encode an expression which uses the current value of the variable
       // s to restore the original value of 13.57 to the variable f
       f = (float)s+(float)0.57; /*13.57 in f (13 in s is casted to float and 0.57 which compiler sees as double is also casted to float)*/
       System.out.println("f is now " + f);
      
       // encode an output statement which uses a ternary conditional operator
       // to compare the variables s and f and prints "f > s" if f is greater
       // than s, or "s > f" if s is greater than f
       System.out.println((f>s)?"f > s":"s > f");
      
       // declare and instantiate a SunFlower variable named sf
       SunFlower sf =new SunFlower() ;
      
       // declare a Flower reference variable and assign the variable
       // sf as its initial value
       Flower flow = sf;
      
       // declare a Flower reference variable and instantiate it as a BlackEyedSusan
       Flower flow2 = new BlackEyedSusan();
       // Declare a BlackEyedSusan reference variable and
       // assign the variable flow2 as its initial value
       BlackEyedSusan bes =(BlackEyedSusan) flow2 ; /*since there is no connected between BlackEyedSusan and Flower class casting is needed else compiler will show exception*/
       }
}

output when above code run is :

to format your code when you paste above code--> Ctrl+a then Ctrl+Shift+f

Know the answer?
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
Similar Questions