[go: up one dir, main page]

0% found this document useful (0 votes)
11 views3 pages

Exxp 5

The document contains a Scala program that defines a 'Person' class and a 'Student' class that inherits from it. The 'StudentApp' object allows users to input and update student information such as name, age, country, and grade. The program prints both the original and updated student details to the console.

Uploaded by

apoorva
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views3 pages

Exxp 5

The document contains a Scala program that defines a 'Person' class and a 'Student' class that inherits from it. The 'StudentApp' object allows users to input and update student information such as name, age, country, and grade. The program prints both the original and updated student details to the console.

Uploaded by

apoorva
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

import scala.io.StdIn.

readLine

class Person(var name: String, var age: Int, var country: String) {

def getName: String = name

def setName(newName: String): Unit = {

name = newName

def getAge: Int = age

def setAge(newAge: Int): Unit = {

age = newAge

def getCountry: String = country

def setCountry(newCountry: String): Unit = {

country = newCountry

class Student(name: String, age: Int, country: String, var grade: String)

extends Person(name, age, country) {

def getGrade: String = grade

def setGrade(newGrade: String): Unit = {

grade = newGrade

}
object StudentApp {

def main(args: Array[String]): Unit = {

// Initial input

println("Enter student's name:")

val name = readLine()

println("Enter student's age:")

val age = readLine().toInt

println("Enter student's country:")

val country = readLine()

println("Enter student's grade:")

val grade = readLine()

val student = new Student(name, age, country, grade)

// Print original student info

println("\nOriginal Student:")

println(s"Name: ${student.getName}")

println(s"Age: ${student.getAge}")

println(s"Country: ${student.getCountry}")

println(s"Grade: ${student.getGrade}")

// Update input using setters

println("\n-- Update Student Info --")

println("Enter new name:")

val newName = readLine()

student.setName(newName)
println("Enter new age:")

val newAge = readLine().toInt

student.setAge(newAge)

println("Enter new country:")

val newCountry = readLine()

student.setCountry(newCountry)

println("Enter new grade:")

val newGrade = readLine()

student.setGrade(newGrade)

// Print updated student info

println("\nUpdated Student:")

println(s"Name: ${student.getName}")

println(s"Age: ${student.getAge}")

println(s"Country: ${student.getCountry}")

println(s"Grade: ${student.getGrade}")

You might also like