Home > OS >  How to Convert this code from Java to Scala
How to Convert this code from Java to Scala

Time:06-12

I am trying to change the code from Java to Scala and I dont know how to do it. Below attached the code that I'm trying to change. I would like to know how to change it and thanks a lot for your time.

// Java program to Illustrate Concept of Composition
 
// Importing required classes
import java.io.*;
import java.util.*;
 
// Class 1
// Helper class
// Book class
class Book {
 
    // Member variables of this class
    public String title;
    public String author;
 
    // Constructor of this class
    Book(String title, String author)
    {
 
        // This keyword refers top current instance
        this.title = title;
        this.author = author;
    }
}
 
// Class 2
// Helper class
// Library class contains list of books.
class Library {
 
    // Reference to refer to list of books.
    private final List<Book> books;
 
    // Constructor of this class
    Library(List<Book> books)
    {
 
        // This keyword refers to current instance itself
        this.books = books;
    }
 
    // Method of this class
    // Getting the list of books
    public List<Book> getListOfBooksInLibrary()
    {
        return books;
    }
}
 
// Class 3
// Main class
class GFG {
 
    // Main driver method
    public static void main(String[] args)
    {
 
        // Creating the objects of class 1 (Book class)
        // inside main() method
        Book b1
            = new Book("EffectiveJ Java", "Joshua Bloch");
        Book b2
            = new Book("Thinking in Java", "Bruce Eckel");
        Book b3 = new Book("Java: The Complete Reference",
                           "Herbert Schildt");
 
        // Creating the list which contains the
        // no. of books.
        List<Book> book = new ArrayList<Book>();
 
        // Adding books to List object
        // using standard add() method
        book.add(b1);
        book.add(b2);
        book.add(b3);
 
        // Creating an object of class 2
        Library library = new Library(book);
 
        // Calling method of class 2 and storing list of
        // books in List Here List is declared of type
        // Books(user-defined)
        List<Book> books
            = library.getListOfBooksInLibrary();
 
        // Iterating over for each loop
        for (Book bk : books) {
 
            // Print and display the title and author of
            // books inside List object
            System.out.println("Title : "   bk.title
                                 " and "
                                 " Author : "   bk.author);
        }
    }
}

Basically, I would like it to execute the same thing but in Scala but I dont really know how. Appreciat it if you could show me how to do it. Thank you

This is another simple java code. I still dont really get how so I now pick a easier one for understanding. Thank you.

 public class Subject {
private String name;
public void setName(String name)  {
this.name = name;
}
public String getName()
{
return name;
}
}
public class Student {
private Subject[] studyAreas = new Subject[10];
//the rest of the Student class
} 

Thank you for helping me to understand. I sincerely appreciate it alot.

CodePudding user response:

This's all(save it as xxx.scala, and run with scala xxx.scala):

case class Book(title: String, author: String)

case class Library(books: List[Book])

object GFG {
  def main(args: Array[String]): Unit = {
     val b1 = Book("EffectiveJ Java", "Joshua Bloch")
     val b2 = Book("Thinking in Java", "Bruce Eckel")
     val b3 = Book("Java: The Complete Reference", "Herbert Schildt")
     val book = List(b1, b2, b3)
     val library = Library(book)
     val books = library.books
     for (bk <- books) println(s"Title : ${bk.title}  and  Author : ${bk.author}")
  }
}

CodePudding user response:

import scala.collection.mutable._;
// Class 1
// Helper class
// Book class
class Book (
// Member variables of this class
var title : String = null, var author : String = null)
{
    // Constructor of this class
    def this(title : String, author : String)
    {
        // This keyword refers top current instance
        this(title, author)
    }
}
// Class 2
// Helper class
// Library class contains list of books.
class Library (
// Reference to refer to list of books.
var books : vector[Book] = null)
{
    // Constructor of this class
    def this(books : vector[Book])
    {
        // This keyword refers to current instance itself
        this(books)
    }
    // Method of this class
    // Getting the list of books
    def getListOfBooksInLibrary() : vector[Book]=
    {
        return books
    }
}
// Class 3
// Main class
class GFG ()
{
    // Main driver method
}

object Main 
{
    def main(args : Array[String]) : Unit=
    {
        // Creating the objects of class 1 (Book class)
        // inside main() method
        var b1 = new Book("EffectiveJ Java", "Joshua Bloch")
        var b2 = new Book("Thinking in Java", "Bruce Eckel")
        var b3 = new Book("Java: The Complete Reference", "Herbert Schildt")
        // Creating the list which contains the
        // no. of books.
        var book = new ArrayBuffer[Book]()
        // Adding books to List object
        // using standard add() method
        book  = b1
        book  = b2
        book  = b3
        // Creating an object of class 2
        var library = new Library(book)
        // Calling method of class 2 and storing list of
        // books in List Here List is declared of type
        // Books(user-defined)
        var books = library.getListOfBooksInLibrary()
        // Iterating over for each loop
        for (var bk : books)
        {
            // Print and display the title and author of
            // books inside List object
            println("Title : "   bk.title   " and "   " Author : "   bk.author)
        }
    }
}
  • Related