Programming and Program Design Java Assignment, 7COM1015, Sem A
VerifiedAdded on 2022/08/29
|28
|6556
|27
Homework Assignment
AI Summary
This document presents a comprehensive solution to a Java programming assignment, likely for a university-level course such as 7COM1015 (Programming and Program Design) at the University of Hertfordshire. The assignment covers a wide range of Java programming concepts, including variable declaration, data types, assignment operations, and object-oriented design principles such as inheritance and polymorphism. The solution is divided into two sections, with the first section containing short answer questions that test understanding of fundamental Java syntax and concepts, and the second section containing more in-depth questions that require the creation of Java classes and methods. The solutions provided demonstrate the correct implementation of these concepts and provide a valuable resource for students studying Java programming and related subjects. The provided assignment also covers topics such as method overloading, constructors, and the use of arrays and loops. The document serves as an excellent study aid and provides practical examples of how to apply Java programming principles to solve real-world problems. Desklib is a platform that provides students with valuable resources for their studies, including past papers and solved assignments.

(2016/17) SEMESTER – A
SECTION-A:
Answer 1: boolean rich = true;
Answer 2: count ++; this is the post increment, first execute the execution then increment the
value.
Answer 3: Primitive types are byte, short, char, int, float, double and long, these are considered
as a primitive type, other than these types are object types, such as String, File, Scanner etc. The
main difference between is primitive types are always has a value, it can be never null, but in
case of object types it can be null.
Answer 4:
Scanner sc = new Scanner(System.in);
int mark = sc.nextInt();
if(mark >= 70)
System.out.println(“distinction”);
if(mark>=45 && mark<70)
System.out.println(“pass”);
Answer 5: There is no output, because condition false, at the time of first condition check.
Answer 6:
int complaints [] = new int[12];
Scanner sc = new Scanner(System.in);
for(int x = 0; x<12;x++)
{
complaints[x] = sc.nextInt();
}
SECTION-A:
Answer 1: boolean rich = true;
Answer 2: count ++; this is the post increment, first execute the execution then increment the
value.
Answer 3: Primitive types are byte, short, char, int, float, double and long, these are considered
as a primitive type, other than these types are object types, such as String, File, Scanner etc. The
main difference between is primitive types are always has a value, it can be never null, but in
case of object types it can be null.
Answer 4:
Scanner sc = new Scanner(System.in);
int mark = sc.nextInt();
if(mark >= 70)
System.out.println(“distinction”);
if(mark>=45 && mark<70)
System.out.println(“pass”);
Answer 5: There is no output, because condition false, at the time of first condition check.
Answer 6:
int complaints [] = new int[12];
Scanner sc = new Scanner(System.in);
for(int x = 0; x<12;x++)
{
complaints[x] = sc.nextInt();
}
Paraphrase This Document
Need a fresh take? Get an instant paraphrase of this document with our AI Paraphraser

Answer 7: private double getBill(int quantity, double price) {
double totalCost = quantity * price;
return totalCost;
}
Answer 8: In a class, two methods with same name and different argument types then, it is called
method overloading. Example:
public class Demo {
public int multiply(int a, int b)
{
return a*b;
}
public int multiply(int a)
{
return a*a;
}
}
Answer 9: Student student = new Student();
The given statement is that, it create object of Student class with default constructor by using
new keyword in java.
Answer 10:
for(Student student: msc)
{
if(student.getSRN() == 123456)
{
System.out.println(student.toString());
}
}
Answer 12: class A is a client of class B means, class B is an abstract class and class A is provide
implementation of the abstract of class B.
double totalCost = quantity * price;
return totalCost;
}
Answer 8: In a class, two methods with same name and different argument types then, it is called
method overloading. Example:
public class Demo {
public int multiply(int a, int b)
{
return a*b;
}
public int multiply(int a)
{
return a*a;
}
}
Answer 9: Student student = new Student();
The given statement is that, it create object of Student class with default constructor by using
new keyword in java.
Answer 10:
for(Student student: msc)
{
if(student.getSRN() == 123456)
{
System.out.println(student.toString());
}
}
Answer 12: class A is a client of class B means, class B is an abstract class and class A is provide
implementation of the abstract of class B.

Answer 13: MscStudent inherit from the Student class, means Student class is parent class and
MscStudent is a child class. MscStudent use all method of parent Student class but Student
parent class not use any method of its child class. If MscStudent class not satisfied with the
parent class implementation then MscStudent class should override that method inside our class
and write our logic in it.
Answer 14: public String toString(), this method comes from the Parent class means Object
class, to provide String representation of the object, if we are not satisfied with the default
implementation, then you can override it in our class.
Answer 15: If StockItem class declared with the abstract keyword, then its client classes should
implement that methods which are abstract in the StockItem class, If client classes not implement
abstract method of StockItem class then client class should declared with abstract keyword and
its client class should implement those methods which are abstract.
SECTION-B
Answer 16: (a)
public class Vehicle {
private String registration;
private int mileage;
private double costPerMile;
public Vehicle () {
}
public Vehicle (String registration, int mileage, double costPerMile) {
this. registration = registration;
this. mileage = mileage;
this. costPerMile = costPerMile;
}
}
public class Car extends Vehicle
{
private boolean hatchback;
MscStudent is a child class. MscStudent use all method of parent Student class but Student
parent class not use any method of its child class. If MscStudent class not satisfied with the
parent class implementation then MscStudent class should override that method inside our class
and write our logic in it.
Answer 14: public String toString(), this method comes from the Parent class means Object
class, to provide String representation of the object, if we are not satisfied with the default
implementation, then you can override it in our class.
Answer 15: If StockItem class declared with the abstract keyword, then its client classes should
implement that methods which are abstract in the StockItem class, If client classes not implement
abstract method of StockItem class then client class should declared with abstract keyword and
its client class should implement those methods which are abstract.
SECTION-B
Answer 16: (a)
public class Vehicle {
private String registration;
private int mileage;
private double costPerMile;
public Vehicle () {
}
public Vehicle (String registration, int mileage, double costPerMile) {
this. registration = registration;
this. mileage = mileage;
this. costPerMile = costPerMile;
}
}
public class Car extends Vehicle
{
private boolean hatchback;
⊘ This is a preview!⊘
Do you want full access?
Subscribe today to unlock all pages.

Trusted by 1+ million students worldwide

public Car() {
super();
}
public Car(String registration, int mileage,double costPerMile, boolean hatchback) {
super(registration, mileage, costPerMile);
this. hatchback = hatchback;
}
}
public class Van extends Vehicle
{
private int maxCapacity;
private boolean sideDoor;
public Van () {
super();
}
public Van (String registration, int mileage, String make, double costPerMile, int
maxCapacity, boolean sideDoor) {
super(registration, mileage, costPerMile);
this. maxCapacity = maxCapacity;
this. sideDoor = sideDoor;
}
}
(b) public abstract double getCostPerMile();
i) If add this method in the Vehicle class, Vehicle class should be declared with abstract keword.
ii) The subclass Car or Van should provide the implement of the parent class abstract method
otherwise make subclass also abstract class and provide further implementation in its child class,
if not provide implementation, its gives error.
super();
}
public Car(String registration, int mileage,double costPerMile, boolean hatchback) {
super(registration, mileage, costPerMile);
this. hatchback = hatchback;
}
}
public class Van extends Vehicle
{
private int maxCapacity;
private boolean sideDoor;
public Van () {
super();
}
public Van (String registration, int mileage, String make, double costPerMile, int
maxCapacity, boolean sideDoor) {
super(registration, mileage, costPerMile);
this. maxCapacity = maxCapacity;
this. sideDoor = sideDoor;
}
}
(b) public abstract double getCostPerMile();
i) If add this method in the Vehicle class, Vehicle class should be declared with abstract keword.
ii) The subclass Car or Van should provide the implement of the parent class abstract method
otherwise make subclass also abstract class and provide further implementation in its child class,
if not provide implementation, its gives error.
Paraphrase This Document
Need a fresh take? Get an instant paraphrase of this document with our AI Paraphraser

iii) import java.util.ArrayList;
public class Garage {
public static void main(String[] args) {
ArrayList< Vehicle > collection = new ArrayList<>();
Vehicle vehicle1 = new Car("C1234", 100, 70.5, true);
Vehicle vehicle2 = new Van("V1234", 140, ,60.5 20, true);
collection.add(vehicle1);
collection.add(vehicle2);
}
}
(2016/17) SEMESTER -B
SECTION_A
Answer 1: double num = 19.567;
Answer 2: The value of wk = 7
Answer 3: * means for multiplication, % means for modulus
Answer 5: The value of y is 4 and Output is 9
Answer 6: int array[] = new int[4];
array[4] = 0; when this line is executed, it gives runtime error, says:
java.lang.ArrayIndexOutOfBoundsException: 4
Answer 7: Student csit1[] = new Student[150];
Student student1 = new Student(“John”, “Harry”);
public class Garage {
public static void main(String[] args) {
ArrayList< Vehicle > collection = new ArrayList<>();
Vehicle vehicle1 = new Car("C1234", 100, 70.5, true);
Vehicle vehicle2 = new Van("V1234", 140, ,60.5 20, true);
collection.add(vehicle1);
collection.add(vehicle2);
}
}
(2016/17) SEMESTER -B
SECTION_A
Answer 1: double num = 19.567;
Answer 2: The value of wk = 7
Answer 3: * means for multiplication, % means for modulus
Answer 5: The value of y is 4 and Output is 9
Answer 6: int array[] = new int[4];
array[4] = 0; when this line is executed, it gives runtime error, says:
java.lang.ArrayIndexOutOfBoundsException: 4
Answer 7: Student csit1[] = new Student[150];
Student student1 = new Student(“John”, “Harry”);

csit1[0] = student1;
Answer 8: This line: public static void main(String[] args);
public: means you can access this method globally,
static: keyword means call by using its Class name,
void: means its return type is void, nothing return
main: main is the name when by default program start with this main and follow above signature.
String[] args: this is a command line argument, for passing values or initial the variable.
Answer 9: The difference between == and = is that, == operator for check equality between two
values, but only single = equal operate is used for the assigning the values. Example:
int x = 10;
while(x == 10)
{
System.out.println(“X: ”+x);
x++;
}
Answer 10:
public double getCost(double price, int quantity) {
double totalCost = price * quantity;
return totalCost;
}
Answer 11: The main difference between actual and formal parameter is that, actual parameter
means when call any method and pass value in it, these are actual parameters, formal parameter
means when define any method with argument type in it, is formal parameters.
Answer 8: This line: public static void main(String[] args);
public: means you can access this method globally,
static: keyword means call by using its Class name,
void: means its return type is void, nothing return
main: main is the name when by default program start with this main and follow above signature.
String[] args: this is a command line argument, for passing values or initial the variable.
Answer 9: The difference between == and = is that, == operator for check equality between two
values, but only single = equal operate is used for the assigning the values. Example:
int x = 10;
while(x == 10)
{
System.out.println(“X: ”+x);
x++;
}
Answer 10:
public double getCost(double price, int quantity) {
double totalCost = price * quantity;
return totalCost;
}
Answer 11: The main difference between actual and formal parameter is that, actual parameter
means when call any method and pass value in it, these are actual parameters, formal parameter
means when define any method with argument type in it, is formal parameters.
⊘ This is a preview!⊘
Do you want full access?
Subscribe today to unlock all pages.

Trusted by 1+ million students worldwide

Exmaple: public class Demo {
public static void sum(int a, int b)
{
System.out.println(“The sum is: ”+(a+b));
}
public static void main(String[] args)
{
int x = 10;
int y = 20;
sum(x,y);
}
Actual Parameter: x, y
Formal Parameter: a,b
Answer 13: In constructor, is define under in a class, its scope is as same as the class scope. In
the constructor to initialize the variable, because constructor is executed before its main method.
SECTION-B
Answer 16: (a)
public class Copier {
private String id;
private int maxCopies;
private double costPerPage;
public Copier () {
public static void sum(int a, int b)
{
System.out.println(“The sum is: ”+(a+b));
}
public static void main(String[] args)
{
int x = 10;
int y = 20;
sum(x,y);
}
Actual Parameter: x, y
Formal Parameter: a,b
Answer 13: In constructor, is define under in a class, its scope is as same as the class scope. In
the constructor to initialize the variable, because constructor is executed before its main method.
SECTION-B
Answer 16: (a)
public class Copier {
private String id;
private int maxCopies;
private double costPerPage;
public Copier () {
Paraphrase This Document
Need a fresh take? Get an instant paraphrase of this document with our AI Paraphraser

}
public Copier (String id, int maxCopies, double costPerPage) {
this.id = id;
this. maxCopies = maxCopies;
this. costPerPage = costPerPage;
}
}
public class Inkjet extends Copier
{
private boolean photo;
public Inkjet () {
super();
}
public Inkjet (String id, int maxCopies,double costPerPage, boolean photo) {
super(id, maxCopies, costPerPage);
this. photo = photo;
}
}
public class Laser extends Copier
{
private boolean remotePrint;
public Laser () {
super();
}
public Laser (String id, int maxCopies, double costPerPage, boolean remotePrint) {
public Copier (String id, int maxCopies, double costPerPage) {
this.id = id;
this. maxCopies = maxCopies;
this. costPerPage = costPerPage;
}
}
public class Inkjet extends Copier
{
private boolean photo;
public Inkjet () {
super();
}
public Inkjet (String id, int maxCopies,double costPerPage, boolean photo) {
super(id, maxCopies, costPerPage);
this. photo = photo;
}
}
public class Laser extends Copier
{
private boolean remotePrint;
public Laser () {
super();
}
public Laser (String id, int maxCopies, double costPerPage, boolean remotePrint) {

super(id, maxCopies, costPerPage);
this. remotePrint = remotePrint;
}
}
(b) public abstract double getCostPerPage();
i) If add this method in the Copier class, Vehicle class should be declared with abstract keword.
ii) The subclass Inkjet or Laser should provide the implement of the parent class abstract method
otherwise make subclass also abstract class and provide further implementation in its child class,
if not provide implementation, its gives error.
iii) import java.util.ArrayList;
public class PrintRoom{
public static void main(String[] args) {
ArrayList< Copier > collection = new ArrayList<>();
Copier copier1 = new Inkjet ("I1234", 10, 2.5, true);
Copier copier2 = new Laser ("L1234", 20, 1.5,true);
collection.add(copier1);
collection.add(copier2);
}
}
(2017/2018) – SEMESTER – A
Answer 1: double price = 1.25;
Answer 2: bonus = bonus + 3, this line execute then gives error at semicolon mission in end of
the line, if add semi colon in the end if the line, then line execute, first in bonus variable increase
by 3 then assign to count variable.
Answer 3: The difference between primitive types and object types are as given below:-
this. remotePrint = remotePrint;
}
}
(b) public abstract double getCostPerPage();
i) If add this method in the Copier class, Vehicle class should be declared with abstract keword.
ii) The subclass Inkjet or Laser should provide the implement of the parent class abstract method
otherwise make subclass also abstract class and provide further implementation in its child class,
if not provide implementation, its gives error.
iii) import java.util.ArrayList;
public class PrintRoom{
public static void main(String[] args) {
ArrayList< Copier > collection = new ArrayList<>();
Copier copier1 = new Inkjet ("I1234", 10, 2.5, true);
Copier copier2 = new Laser ("L1234", 20, 1.5,true);
collection.add(copier1);
collection.add(copier2);
}
}
(2017/2018) – SEMESTER – A
Answer 1: double price = 1.25;
Answer 2: bonus = bonus + 3, this line execute then gives error at semicolon mission in end of
the line, if add semi colon in the end if the line, then line execute, first in bonus variable increase
by 3 then assign to count variable.
Answer 3: The difference between primitive types and object types are as given below:-
⊘ This is a preview!⊘
Do you want full access?
Subscribe today to unlock all pages.

Trusted by 1+ million students worldwide

Primitive types are byte, short, char, int, float, double and long, these are considered as a
primitive type, other than these types are object types, such as String, File, Scanner etc. The main
difference between is primitive types are always has a value, it can be never null, but in case of
object types it can be null.
Answer 4:
if(score >=100 && score<500){
System.out.println(“To win a game”);
}
if(score>=500)
{
System.out.println(“Get a free game”);
}
Answer 5: In the given code, it gives error at compile time because in the first line:
int number = 10
there is no semicolon at the end of the line. If we add semicolon at the end of the line then the
output is 14.
Answer 6: int cutomers[] = new int[7];
Answer 7:
public double getTotalCost(double ticketCost, int numberOfPeople) {
double totalCost = ticketCost / numberOfPeople;
return totalCost;
}
Answer 8: Overloading means in a class two or more methods with same name but different
argument types. Example :
public class Sample {
public void sum(int a, int b)
primitive type, other than these types are object types, such as String, File, Scanner etc. The main
difference between is primitive types are always has a value, it can be never null, but in case of
object types it can be null.
Answer 4:
if(score >=100 && score<500){
System.out.println(“To win a game”);
}
if(score>=500)
{
System.out.println(“Get a free game”);
}
Answer 5: In the given code, it gives error at compile time because in the first line:
int number = 10
there is no semicolon at the end of the line. If we add semicolon at the end of the line then the
output is 14.
Answer 6: int cutomers[] = new int[7];
Answer 7:
public double getTotalCost(double ticketCost, int numberOfPeople) {
double totalCost = ticketCost / numberOfPeople;
return totalCost;
}
Answer 8: Overloading means in a class two or more methods with same name but different
argument types. Example :
public class Sample {
public void sum(int a, int b)
Paraphrase This Document
Need a fresh take? Get an instant paraphrase of this document with our AI Paraphraser

{}
public void sum(int a, int b, int c )
{}
}
Answer 9:
public class StockItem{
private int stockNumber;
public StockItem(int stockNumber)
{
this.stockNumber = stockNumber;
}
}
public class Warehouse {
public static void main(String[] args)
{
int stockNumber = 10;
StockItem item1 = new StockItem(stockNumber);
}
}
Answer 10:
for(Patient patient: waiting)
{
if(patient.getWaitingTime () > 6)
{
System.out.println(patient.toString());
}
}
Answer 11: class A is a supplier of class B means, class B is an abstract class and class A is
provide implementation of the abstract of class B.
Answer 12: Lecturer inherit from the Person class, means Person class is parent class and
Lecturer is a child class. Lecturer use all method of parent Person class but Person parent class
not use any method of its child class. If Lecturer class not satisfied with the parent class
public void sum(int a, int b, int c )
{}
}
Answer 9:
public class StockItem{
private int stockNumber;
public StockItem(int stockNumber)
{
this.stockNumber = stockNumber;
}
}
public class Warehouse {
public static void main(String[] args)
{
int stockNumber = 10;
StockItem item1 = new StockItem(stockNumber);
}
}
Answer 10:
for(Patient patient: waiting)
{
if(patient.getWaitingTime () > 6)
{
System.out.println(patient.toString());
}
}
Answer 11: class A is a supplier of class B means, class B is an abstract class and class A is
provide implementation of the abstract of class B.
Answer 12: Lecturer inherit from the Person class, means Person class is parent class and
Lecturer is a child class. Lecturer use all method of parent Person class but Person parent class
not use any method of its child class. If Lecturer class not satisfied with the parent class

implementation then Lecturer class should override that method inside our class and write our
logic in it.
Answer 13: public String toString(), this method comes from the Parent class means Object
class, to provide String representation of the object, if we are not satisfied with the default
implementation, then you can override it in our class.
Answer 14: public : when public modifier is used in the method then you can access by
anywhere of the project, by using its class object reference or it is static then you can by Class
name with dot and method name.
private : private modifier is used in the method, then on one can access this method outside the
class, only access inside the class.
static : static modifier is used in the method to make to it class level method, you can access this
method inside any method of the class.
(2017/2018) – SEMESTER - B
SECTION-A
Answer 1: String name = “fred”;
Answer 2: count = count + 1, this line execute then gives error at semicolon mission in end of the
line, if add semi colon in the end if the line, then line execute, first in count variable increase by
2 then assign to count variable.
Answer 3: Primitive types are byte, short, char, int, float, double and long, these are considered
as a primitive type, other than these types are object types, such as String, File, Scanner etc. The
main difference between is primitive types are always has a value, it can be never null, but in
case of object types it can be null.
Answer 4:
Scanner sc = new Scanner(System.in);
int age = sc.nextInt();
if(age >= 18)
System.out.println(“A person can vote”);
if(age>=18 && age<60)
System.out.println(“Eligible for jury service”);
Answer 5: In the given code, it gives error at compile time because in the first line:
logic in it.
Answer 13: public String toString(), this method comes from the Parent class means Object
class, to provide String representation of the object, if we are not satisfied with the default
implementation, then you can override it in our class.
Answer 14: public : when public modifier is used in the method then you can access by
anywhere of the project, by using its class object reference or it is static then you can by Class
name with dot and method name.
private : private modifier is used in the method, then on one can access this method outside the
class, only access inside the class.
static : static modifier is used in the method to make to it class level method, you can access this
method inside any method of the class.
(2017/2018) – SEMESTER - B
SECTION-A
Answer 1: String name = “fred”;
Answer 2: count = count + 1, this line execute then gives error at semicolon mission in end of the
line, if add semi colon in the end if the line, then line execute, first in count variable increase by
2 then assign to count variable.
Answer 3: Primitive types are byte, short, char, int, float, double and long, these are considered
as a primitive type, other than these types are object types, such as String, File, Scanner etc. The
main difference between is primitive types are always has a value, it can be never null, but in
case of object types it can be null.
Answer 4:
Scanner sc = new Scanner(System.in);
int age = sc.nextInt();
if(age >= 18)
System.out.println(“A person can vote”);
if(age>=18 && age<60)
System.out.println(“Eligible for jury service”);
Answer 5: In the given code, it gives error at compile time because in the first line:
⊘ This is a preview!⊘
Do you want full access?
Subscribe today to unlock all pages.

Trusted by 1+ million students worldwide
1 out of 28
Your All-in-One AI-Powered Toolkit for Academic Success.
+13062052269
info@desklib.com
Available 24*7 on WhatsApp / Email
Unlock your academic potential
Copyright © 2020–2025 A2Z Services. All Rights Reserved. Developed and managed by ZUCOL.