Week 4: Programming Assignment 1
Complete the code segment to execute the following program successfully. You should import the correct package(s) and/or class(s) to complete the code.
// Import the required package(s) and/or class(es)
import java.lang.String;
import static java.lang.System.*;
import java.util.Scanner;
// main class Question is created
public class Question41{
public static void main(String[] args) {
// Scanner object is created
Scanner scanner = new Scanner(System.in);
// Read String input using scanner class
String courseName = scanner.nextLine();
// Print the scanned String
out.println("Course: " + courseName);
}
}
Week 4: Programming Assignment 2
Complete the code segment to print the current year. Your code should compile successfully.
Note: In this program, you are not allowed to use any import statement. Use should use predefined class Calendar defined in java.util package.
// The following is the declaration of the main class named Question42
public class Question42 {
public static void main(String args[]){
int year; // integer type variable to store year
// Create an object of Calendar class.
java.util.Calendar obj;
// Use getInstance() method to initialize the Calendar object.
obj=java.util.Calendar.getInstance();
//obj=new java.util.Calendar();
// Initialize the int variable year with the current year
year=obj.get(obj.YEAR);
// Print the current Year
System.out.println("Current Year: "+year);
}
}
Week 4: Programming Assignment 3
The program in this assignment is attempted to print the following output:
-----------------OUTPUT-------------------
This is large
This is medium
This is small
This is extra-large
-------------------------------------------------
However, the code is intentionally injected with some bugs. Debug the code to execute the program successfully.
interface ExtraLarge{
String extra = "This is extra-large";
public void display();
}
class Large {
public void Print() {
System.out.println("This is large");
}
}
class Medium extends Large {
public void Print() {
super.Print();
System.out.println("This is medium");
}
}
class Small extends Medium {
public void Print() {
super.Print();
System.out.println("This is small");
}
}
class Question43{
public static void main(String[] args) {
Small s = new Small();
s.Print();
Question43 q = new Question43();
q.display();
}
public void display()
{
System.out.print(ExtraLarge.extra);
}
}
Week 4: Programming Assignment 4
Complete the code segment to call the default method in the interface First and Second.
interface First{
// default method
default void show(){
System.out.println("Default method implementation of First interface.");
}
}
interface Second{
// Default method
default void show(){
System.out.println("Default method implementation of Second interface.");
}
}
// Implementation class code
class Question44 implements First, Second{
// Overriding default show method
public void show(){
// Call show() of First interface.
First.super.show();
// Call show() of Second interface.
Second.super.show();
}
public static void main(String args[]){
Question44 q = new Question44();
q.show();
}
}
Week 4: Programming Assignment 5
Modify the code segment to print the following output.
-----------------OUTPUT-------------------
Circle: This is Shape1
Circle: This is Shape2
-------------------------------------------------
// Interface ShapeX is created
interface ShapeX {
public String base = "This is Shape1";
public void display1();
}
// Interface ShapeY is created which extends ShapeX
interface ShapeY extends ShapeX {
public String base = "This is Shape2";
public void display2();
}
// Class ShapeG is created which implements ShapeY
class ShapeG implements ShapeY {
public String base = "This is Shape3";
//Overriding method in ShapeX interface
public void display1() {
System.out.println("Circle: " + ShapeX.base);
}
// Override method in ShapeY interface
public void display2()
{
System.out.print("Circle: " + ShapeY.base);
}
}
// Main class Question
public class Question45{
public static void main(String[] args) {
//Object of class shapeG is created and display methods are called.
ShapeG circle = new ShapeG();
circle.display1();
circle.display2();
}
}
Previous :Week 4 - Assignment