Question

In: Computer Science

fun main() {     val stringResult = getResult("Kotlin")     val intResult = getResult(100)     // TODO...

fun main() {
    val stringResult = getResult("Kotlin")
    val intResult = getResult(100)

    // TODO 2
    println()
}

// TODO 1
fun <T> getResult(args: T): Int {
    return 0
}

TODO 1:
Create a new generics function with the following conditions:
The function name is getResult.
Has one type of parameter.
If the attached argument is of type Int, then the value returned is the value of the argument multiplied by 5.
If the attached argument is of type String, the value returned is a character length.
If the attached argument is of type other than Int and String, then the returned value is 0.

TODO 2:
Add functions to print the values of the stringResult and intResult variables in the console.

Solutions

Expert Solution

Hi,

I have written two methods one for TODO1, another for TODO2.

TODO2 method name is : printIntResult

it will just print the argument passed in it.

TODO1 it is generic method

it can take any type of argument as you can see code below.

Please find the attached code and output also , hope it will help you.

class TODO {
   public static void main(String[] args){
       int stringResult = getResult("Kotlin");
       int intResult = getResult(100);

// TODO 2
       printIntResult(stringResult);
       printIntResult(intResult);
   }
  
   //TODO2 method
   public static void printIntResult(int val){
       System.out.println(val);
   }
  
   /**
   TODO 1 - method
   this method can take any type of argument
   it will always return integer as per your requirement.
   **/
   public static int getResult(Object obj){
       if(obj instanceof Integer){
           return ((Integer)obj) * 5;
       }
       else if(obj instanceof String){
           String newString = (String)obj;
           return newString.length();
       }
       else
           return 0;
   }
  
}

when you will run this code :

output will be below image :


Related Solutions

anwer in kotlin fun main() { val text = "Kotlin".getFirstAndLast() val firstChar = text["first"] val lastChar...
anwer in kotlin fun main() { val text = "Kotlin".getFirstAndLast() val firstChar = text["first"] val lastChar = text["last"] // TODO 2 println() } // TODO 1 fun String.getFirstAndLast() = mapOf<String, Char>() TODO 1: Make the getFirstAndLast function the extension of the String class with the return type Map <String, Char> TODO 2: Add functions to print the values of the firstChar and lastChar variables in the console. there no more information from this task i dont know. this is the...
// TODO 1 class Cat(private val name: String) { var sleep: Boolean = false fun toSleep()...
// TODO 1 class Cat(private val name: String) { var sleep: Boolean = false fun toSleep() { println() } } fun main() { // TODO 2 val gippy = Cat("") gippy.toSleep() gippy.sleep = true gippy.toSleep() } TODO 1: Complete the code in the Cat class with the following conditions: Create a getter setter function for the sleep property in which there is a function to print text: The getter / setter function is called Add code to the toSleep ()...
ANSWER WITH ML CODE PLZ. NOT C++ 1) fun main() = let val yy = [6,4,2,0];...
ANSWER WITH ML CODE PLZ. NOT C++ 1) fun main() = let val yy = [6,4,2,0]; in print (PolyML.makestring ( ***(Remove the first item from yy)****** )) end; 2) fun main() = let val yy = [6,4,2,0]; in print (PolyML.makestring ( ***(Make a list containing only the first item of yy )*** )) end; 3) fun main() = let val yy = [8,6,4,2,0]; in print (PolyML.makestring ( ***(Prepend 5 to the front of yy)*** )) end; 4)fun main() = let...
import kotlinx.coroutines.* // TODO 1 fun sum(valueA: Int, valueB: Int): Int {     return 0 }...
import kotlinx.coroutines.* // TODO 1 fun sum(valueA: Int, valueB: Int): Int {     return 0 } // TODO 2 fun multiple(valueA: Int, valueB: Int): Int {     return 0 } fun main() = runBlocking {     println("Counting...")     val resultSum = async { sum(10, 10) }     val resultMultiple = async { multiple(20, 20) }     // TODO 3     println() } TODO 1: Change it to suspend function with the following conditions: Has a waiting time of 3 seconds...
public class CashRegisterPartTwo { // TODO: Do not modify the main method. // You can add...
public class CashRegisterPartTwo { // TODO: Do not modify the main method. // You can add comments, but the main method should look exactly like this // when you submit your project. public static void main(String[] args) { runTransaction(); } // TODO: Define the runTranscation method. // runTransaction runs the cash register process from start to finish. However, // it will not contain ALL Of the code. You are going to have to delegate some of // its functionality to...
Question 9 What is produced from the following code segment? val = 0; do { val++;...
Question 9 What is produced from the following code segment? val = 0; do { val++; WriteLine(val); } while (val < 5); Nothing is displayed Outputs 5 thru 9 Outputs 10 Outputs 1 thru 4 Outputs 1 thru 5 Outputs 10 thru 14 Outputs 0 thru 4 Question 10 Assuming val was described as an integer and an initial value of 10, what is produced from the following code segment? do { WriteLine(val); } while (val < 10); Nothing is...
Val and Collins agree that Val will give Collins guitar lessons each week for six months...
Val and Collins agree that Val will give Collins guitar lessons each week for six months and Collins will drive Val to the grocery store each week. Describe the elements that make this a valid contract.
#include <stdio.h> #include <math.h> int fun(int); int main(void)    {     int i = 5, x...
#include <stdio.h> #include <math.h> int fun(int); int main(void)    {     int i = 5, x = 3;     i = fun(x);     printf("%d\n", i);     return 0; } int fun(int i) {      int res = 0;      res = pow (i , 3.0);      return ( res); }
In the protein adenylate kinase, the C-terminal region is a helical, with the sequence Val-Asp-Asp-Val-Phe-Ser-Gln-Val-Cys-Thr-His-Leu-Asp-Thr-Leu-Lys. a)...
In the protein adenylate kinase, the C-terminal region is a helical, with the sequence Val-Asp-Asp-Val-Phe-Ser-Gln-Val-Cys-Thr-His-Leu-Asp-Thr-Leu-Lys. a) Underline the hydrophobic amino acids in the sequence. b) If you identified the correct amino acids you will notice that there is a periodicity of hydrophobic amino acids. What is this periodicity and why would this occur?
which rptoleyic enzye would cleave this peptide into the most fragments? Asn-Met-Thr-Gln-Gly-Arg-Cys-Lys-Val-Asn-Thr-Phe-Val-His-Glu-Leu-Val-Asp-Val-Cys-Phe-Lys-Glu A. Trypsin B.Chymotrypsin C.Thermolysint...
which rptoleyic enzye would cleave this peptide into the most fragments? Asn-Met-Thr-Gln-Gly-Arg-Cys-Lys-Val-Asn-Thr-Phe-Val-His-Glu-Leu-Val-Asp-Val-Cys-Phe-Lys-Glu A. Trypsin B.Chymotrypsin C.Thermolysint D.they would all given an equal amou
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT