Question

Convert this java code to scala

public class Main { public static String checkType (Object obj) { //check if the given obj is of type Person if (obj instance

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

Ans)

CODE:

Converting java code to scala

//remove if not needed
import scala.collection.JavaConversions._
object Main {
def checkType(obj: Onbject): String =
    if (obj.isInstanceOf[Person]) {
      val p: Person = obj.asInstanceOf[Person]
      if (p.vehicle.isInstanceOf[Car]) {
        val c: Car = p.vehicle.asInstanceOf[Car]
        "This is a car with plate: " + c.plate
      } else if (p.vehicle.isInstanceOf[Truck]) {
        val t: Truck = p.vehicle.asInstanceOf[Truck]
        "This is a truck with places: " + t.plate + " and weight capacity: " +
          t.weight_cap
      } else {
        "Unknown vehicle type"
      }
    } else {
      "Unknown object type"
    }
}

SCREENSHOT OF THE CODE:

4 de 1 1/remove if not needed 2 import scala.collection. JavaConversions.- 3. object Main { def checkType(obj: Onbject): Stri

Thank you.

Add a comment
Know the answer?
Add Answer to:
Convert this java code to scala public class Main { public static String checkType (Object obj)...
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
  • What is wrong with the following Java Code. public class TestEdible { abstract class Animal {...

    What is wrong with the following Java Code. public class TestEdible { abstract class Animal { /** Return animal sound */ public abstract String sound(); } class Chicken extends Animal implements Edible { @Override public String howToEat() { return "Chicken: Fry it"; } @Override public String sound() { return "Chicken: cock-a-doodle-doo"; } } class Tiger extends Animal { @Override public String sound() { return "Tiger: RROOAARR"; } } abstract class Fruit implements Edible { // Data fields, constructors, and methods...

  • CONVERT THIS JAVA CODE WITH THE JAVA DOC GUIDELINES WHERE APPROPRIATE. DO NOT CHANGE THE CODE....

    CONVERT THIS JAVA CODE WITH THE JAVA DOC GUIDELINES WHERE APPROPRIATE. DO NOT CHANGE THE CODE. SAME CODE SHOULD BE USED FOR THE DOCUMENTATION PURPOSES. ONLY INSERT THE JAVA DOC. //Vehicle.java public class Vehicle {    private String manufacturer;    private int seat;    private String drivetrain;    private float enginesize;    private float weight; //create getters for the attributes    public String getmanufacturer() {        return manufacturer;    }    public String getdrivetrain() {        return drivetrain;...

  • FIX THE FOLLOWING JAVA CODE public class Errors public static main(String[] args) { float sum int...

    FIX THE FOLLOWING JAVA CODE public class Errors public static main(String[] args) { float sum int a = 27.5, b = 72.99; suma(a, b); System.out.println("Suma = %7:3b", sum); } //end main public static suma(float a, double b) { double suma suma = a + b; } }//end class

  • I need the following code to keep the current output but also include somthing similar to...

    I need the following code to keep the current output but also include somthing similar to this but with the correct details from the code. Truck Details: Skoda 100 Nathan Roy 150.5 3200 Details of Vehicle 1: Honda, 5 cd, owned by Peter England Details of Vehicle 2: Skoda, 100 cd, owned by Nathan Roy, 150.5 lbs load, 3200 tow --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- class Person {     private String name;        public Person()     {        name="None";     }       ...

  • What is the Java output? Part One: class Driver { public static void main(String[] args) {...

    What is the Java output? Part One: class Driver { public static void main(String[] args) { int a = 5; int b = 3; if (a < b || a * 2 < b) System.out.print(a - b); System.out.print(b + a); } } Part Two: class Driver { public static void main(String[] args) { int a = 5; int b = 8; if (a < b) if (a * 2 < b) System.out.print("foo"); else System.out.print("bar"); else System.out.print("buz"); } }

  • public class Exercise{ public static void main(String [] arg){ A b=new B(); A c=new C(); //B...

    public class Exercise{ public static void main(String [] arg){ A b=new B(); A c=new C(); //B myB=new C();//must be a type error //The next assertion verifies that the former line would not typecheck assert !((Object)new C() instanceof B); //C myC=new B();//must be a type error //The next assertion verifies that the former line would not typecheck assert !((Object)new B() instanceof C); } } [???] //what should I enter in place of the question marks to get this to work??

  • Convert into pseudo-code for below code =============================== class Main {    public static void main(String args[])...

    Convert into pseudo-code for below code =============================== class Main {    public static void main(String args[])    {        Scanner s=new Scanner(System.in);        ScoresCircularDoubleLL score=new ScoresCircularDoubleLL();        while(true)        {            System.out.println("1--->Enter a number\n-1--->exit");            System.out.print("Enter your choice:");            int choice=s.nextInt();            if(choice!=-1)            {                System.out.print("Enter the score:");                int number=s.nextInt();                GameEntry entry=new GameEntry(number);   ...

  • public class Exercise{ public static void main(String [] arg){ A a=new D(); B b=new D(); C...

    public class Exercise{ public static void main(String [] arg){ A a=new D(); B b=new D(); C c=new D(); c=a; c=b; //A myA=b;//must be a type error //The next assertion verifies that the former line would not typecheck assert !((Object)new B(){} instanceof A); //B myB=a;//must be a type error //The next assertion verifies that the former line would not typecheck assert !((Object)new A(){} instanceof B); } } [???] //what should I enter in place of the question marks to get this...

  • import java.util.Scanner; public class Age { public static void main(String args[]) { Scanner sc = new...

    import java.util.Scanner; public class Age { public static void main(String args[]) { Scanner sc = new Scanner(System.in); int a; System.out.println("Input your age"); a = sc.nextInt(); boolean mess = isAllowed(a); String mess2 = ?isAllowed(a)return"You are allowed to vote";:"You arent allowed"; String age = personAge(a); personAge(a); } public static String personAge(int age) { String mess = ""; if(age<18) return mess = "You are minor"; else if(age>=18 && age<=22) return mess = "You are legal you can vote"; else if(age>=22 && age<=60) return...

  • Code in java Using the template below: public class Lab03 { public static void main(String[] args)...

    Code in java Using the template below: public class Lab03 { public static void main(String[] args) { ArrayList<Integer> list1 = new ArrayList<Integer>(); Collections.addAll(list1, 1, 3, 5, 5 ); ArrayList<Integer> list2 = new ArrayList<Integer>(); Collections.addAll(list2, 3, 7, 3, 2, 4 ); ArrayList<Integer> result1= uniqueUnion(list1,list2); System.out.println(result1); Integer[] array = new Integer[]{ 29, 28, 27, 16, 15, -14, 3, -2, 2}; ArrayList<Integer> arrayList = new ArrayList<Integer>(Arrays.asList(array)); System.out.printf("The average is: %.2f%n", averagePositive(arrayList)); } // end main public static ArrayList<Integer> uniqueUnion(ArrayList<Integer> list1, ArrayList<Integer> list2) {...

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