+1(978)310-4246 credencewriters@gmail.com
  

Description

College of Computing and Informatics
Assignment 2
Deadline: Day 5/12/2021 @ 23:59
[Total Mark for this Assignment is 5]
Student Details:
Name: ###
ID: ###
CRN: ###
Instructions:
•
•
•
•
•
•
•
•
•
•
You must submit two separate copies (one Word file and one PDF file) using the Assignment Template on
Blackboard via the allocated folder. These files must not be in compressed format.
It is your responsibility to check and make sure that you have uploaded both the correct files.
Zero mark will be given if you try to bypass the SafeAssign (e.g. misspell words, remove spaces between
words, hide characters, use different character sets, convert text into image or languages other than English
or any kind of manipulation).
Email submission will not be accepted.
You are advised to make your work clear and well-presented. This includes filling your information on the cover
page.
You must use this template, failing which will result in zero mark.
You MUST show all your work, and text must not be converted into an image, unless specified otherwise by
the question.
Late submission will result in ZERO mark.
The work should be your own, copying from students or other resources will result in ZERO mark.
Use Times New Roman font for all your answers.
Question One
Pg. 01
Learning
Outcome(s):
Develop a
program based on
specification
using
programming
language
elements
including syntax,
data types,
conditional
statement, control
structures,
procedures,
arrays, objects
and classes
Question One
1.5 Marks
Write a java program that:
a. Create an abstract class named Shape that contains three integers
length, breadth, radius and an empty method named printArea().
b. Provide three classes named Rectangle, Triangle and Circle such that
each one of the classes extends the class Shape.
c.
Each one of the classes contains only the method printArea() that read
the requested values and prints the area of the given shape.
d. Provide a class Main that create a rectangle, a triangle and a cercle then
print their respective areas.
Note: you should Include the screenshot of the program output as a part
of your answer. Otherwise, zero marks will be awarded.
Question Two
Pg. 02
Learning
Outcome(s):
Develop a
program based on
specification
using
programming
language
elements
including syntax,
data types,
conditional
statement, control
structures,
procedures,
arrays, objects
and classes
Question Two
2 Marks
Create a class called Invoice that a hardware store might use to represent an
invoice for an item sold at the store.
a. An Invoice should include four information as instance variables: a part
number (type String), a part description (type String), a quantity of the
item being purchased (type int) and a price per item (double).
b. Your class should have a constructor that initializes the four instance
variables. Provide a set and a get method for each instance variable.
c. In addition, provide a method named getInvoiceAmount that calculates
the invoice amount (i.e., multiplies the quantity by the price per item), then
returns the amount as a double value. If the quantity is not positive, it
should be set to 0. If the price per item is not positive, it should be set to
0.0.
d. Write a tester class named InvoiceTest that demonstrates class Invoice’s
capabilities by creating two invoices then changing and printing their
characteristics.
Note: you should Include the screenshot of the program output as a part
of your answer. Otherwise, zero marks will be awarded.
Question Three
Pg. 03
Learning
Outcome(s):
Question Three
1.5 Marks
Write a complete Java program that do the following:
Explain the basic
principles of
programming,
concept of
language, and
universal
constructs of
programming
languages.
a. Read your student’s ID number, store it in an array named myStudentNO
and print it.
b. find the number of even and odd integers in myStudentNO array
Note: Your program output should look as shown below
Develop a
program based on
specification
using
programming
language
elements
including syntax,
data types,
conditional
statement, control
structures,
procedures,
arrays, objects
and
Note: you should Include the screenshot of the program output as a part
of your answer and you should enter your student id number. Otherwise
zero marks will be awarded.
College of Computing and Informatics
Assignment 2
Deadline: Sunday 5/12/2021 @ 23:59
[Total Mark for this Assignment is 5]
Student Details:
Name: ###
ID: ###
CRN: 10484
Instructions:
•
•
•
•
•
•
•
•
•
•
You must submit two separate copies (one Word file and one PDF file) using the Assignment Template on
Blackboard via the allocated folder. These files must not be in compressed format.
It is your responsibility to check and make sure that you have uploaded both the correct files.
Zero mark will be given if you try to bypass the SafeAssign (e.g. misspell words, remove spaces between
words, hide characters, use different character sets, convert text into image or languages other than English
or any kind of manipulation).
Email submission will not be accepted.
You are advised to make your work clear and well-presented. This includes filling your information on the cover
page.
You must use this template, failing which will result in zero mark.
You MUST show all your work, and text must not be converted into an image, unless specified otherwise by
the question.
Late submission will result in ZERO mark.
The work should be your own, copying from students or other resources will result in ZERO mark.
Use Times New Roman font for all your answers.
Question One
Pg. 01
Learning
Outcome(s):
Develop a
program based on
specification
using
programming
language
elements
including syntax,
data types,
conditional
statement, control
structures,
procedures,
arrays, objects
and classes
Question One
1.5 Marks
Write a java program that:
a. Create an abstract class named Shape that contains three integers
length, breadth, radius and an empty method named printArea().
b. Provide three classes named Rectangle, Triangle and Circle such that
each one of the classes extends the class Shape.
c.
Each one of the classes contains only the method printArea() that read
the requested values and prints the area of the given shape.
d. Provide a class Main that create a rectangle, a triangle and a cercle then
print their respective areas.
Note: you should Include the screenshot of the program output as a part
of your answer. Otherwise, zero marks will be awarded.
Answer
Question One
Pg. 02
Answer
package javaassi2q1;
abstract class Shape {
Abstract
A
Shape
int length;
Class
int breadth;
int radius;
abstract double printArea();
}
package javaassi2q1;
public class Rectangle extends Shape {
Rectangle(int l,int b)
{
length=l;
breadth=b;
}
double printArea() {
return length*breadth;
Three Classes
B and
and PrintArea
C
Method
}
}
package javaassi2q1;
public class Triangle extends Shape {
Triangle(int l,int b)
{
length=l;
breadth=b;
}
double printArea() {
return .5 * length*breadth;
}
Question One
Pg. 03
}
package javaassi2q1;
public class Circle extends Shape {
Circle(int r)
{
radius=r;
}
double printArea() {
return Math.PI * radius * radius;
}
}
package javaassi2q1;
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
int length, breadth, radious;
Scanner sc = new Scanner(System.in);
D
Test Class
System.out.print(“Enter length of rectangle : “);
length = sc.nextInt();
System.out.print(“Enter breadth of rectangle : “);
breadth = sc.nextInt();
Rectangle rectangle = new Rectangle(length, breadth);
System.out.println(“Area of Rectangle : ” +
rectangle.printArea());
System.out.print(“Enter length of triangle : “);
length = sc.nextInt();
Question One
Pg. 04
System.out.print(“Enter breadth of triangle : “);
breadth = sc.nextInt();
Triangle triangle = new Triangle(length, breadth);
System.out.println(“Area of triangle : ” + triangle.printArea());
System.out.print(“Enter radious of Circle : “);
radious = sc.nextInt();
Circle circle = new Circle(radious);
System.out.println(“Area of triangle : ” + circle.printArea());
}
}
Question One
Pg. 05
‫اجابة اخرى للسؤال االول‬
package javaassi2q1;
abstract class Shape {
int length;
int breadth;
int radius;
abstract void printArea();
}
package javaassi2q1;
import java.util.Scanner;
public class Rectangle extends Shape {
public void printArea()
{
Scanner sc = new Scanner(System.in);
System.out.print(“Enter length of rectangle : “);
this.length = sc.nextInt();
System.out.print(“Enter breadth of rectangle : “);
this.breadth = sc.nextInt();
System.out.println(“Area of Rectangle : ” + this.length *
this.breadth);
System.out.println();
}
}
package javaassi2q1;
import java.util.Scanner;
public class Triangle extends Shape {
public void printArea()
{
Scanner sc = new Scanner(System.in);
System.out.print(“Enter length of triangle : “);
this.length = sc.nextInt();
System.out.print(“Enter breadth of triangle : “);
this.breadth = sc.nextInt();
System.out.println(“Area of Triangle : ” + (0.5)*(this.length
* this.breadth));
System.out.println();
}
}
Question One
Pg. 06
package javaassi2q1;
import java.util.Scanner;
public class Circle extends Shape {
public void printArea()
{
Scanner sc = new Scanner(System.in);
System.out.print(“Enter radius of circle : “);
this.radius = sc.nextInt();
System.out.println(“Area of Circle : ” + Math.PI * this.radius
* this.radius);
}
}
package javaassi2q1;
public class Test {
public static void main(String[] args) {
Shape rectangle = new Rectangle();
Shape triangle = new Triangle();
Shape circle = new Circle();
rectangle.printArea();
triangle.printArea();
circle.printArea();
}
}
Question Two
Pg. 07
Learning
Outcome(s):
Develop a
program based on
specification
using
programming
language
elements
including syntax,
data types,
conditional
statement, control
structures,
procedures,
arrays, objects
and classes
Question Two
2 Marks
Create a class called Invoice that a hardware store might use to represent an
invoice for an item sold at the store.
a. An Invoice should include four information as instance variables: a part
number (type String), a part description (type String), a quantity of the
item being purchased (type int) and a price per item (double).
b. Your class should have a constructor that initializes the four instance
variables. Provide a set and a get method for each instance variable.
c. In addition, provide a method named getInvoiceAmount that calculates
the invoice amount (i.e., multiplies the quantity by the price per item), then
returns the amount as a double value. If the quantity is not positive, it
should be set to 0. If the price per item is not positive, it should be set to
0.0.
d. Write a tester class named InvoiceTest that demonstrates class Invoice’s
capabilities by creating two invoices then changing and printing their
characteristics.
Note: you should Include the screenshot of the program output as a part
of your answer. Otherwise, zero marks will be awarded.
Question Two
Pg. 08
Answer
Invoice Class
package javaassi2q2;
public class Invoice {
//A.instance variables
private String partNumber;
private String partDescription;
private int quantity;
private double pricePerItem;
//B.Constructor Method
Invoice(String partNumber, String partDescription, int quantity, double pricePerItem) {
this.partNumber = partNumber;
this.partDescription = partDescription;
//c.Input Validation
if (quantity < 0) { this.quantity = 0; } else { this.quantity = quantity; } if (pricePerItem < 0) { this.pricePerItem = 0; } else { this.pricePerItem = pricePerItem; } } //B.get and set methods public void setPartNumber(String partNumber) { this.partNumber = partNumber; } public void setPartDescription(String partDescription) { this.partDescription = partDescription; } public void setQuantity(int quantity) { this.quantity = quantity; } public void setPricePerItem(double pricePerItem) { this.pricePerItem = pricePerItem; Question Two Pg. 09 } public String getPartNumber() { return partNumber; } public String getPartDescription() { return partDescription; } public int getQuantity() { return quantity; } public double getPricePerItem() { return pricePerItem; } //C.getInvoiceAmount method double getInvoiceAmount() { return this.pricePerItem * this.quantity; } } Invoice Tester Class package javaassi2q2; public class InvoiceTest { public static void main(String[] args) { Invoice invoice1 = new Invoice("PartNumber100", "Keyboard", 5, 225.5); Invoice invoice2 = new Invoice("PartNumber200", "Mouse", 5, 178); System.out.println("Invoice No 1n============="); System.out.printf("The Invoice %s%nThe Desciption is %s%nThe quantity %d%nThe Price is %.1f%nThe Invoice Amount is %.1f%n==========================%n",invoice1.getPartNumber(),invoice1.getPartDescript ion(),invoice1.getQuantity(),invoice1.getPricePerItem(),invoice1.getInvoiceAmount()); invoice1.setQuantity(3); System.out.printf("The Invoice %s%nThe Desciption is %s%nThe quantity %d%nThe Price is %.1f%nThe Invoice Amount is %.1f%n=========================%n",invoice1.getPartNumber(),invoice1.getPartDescriptio n(),invoice1.getQuantity(),invoice1.getPricePerItem(),invoice1.getInvoiceAmount()); Question Two Pg. 10 System.out.println("Invoice No 2n============="); System.out.printf("The Invoice %s%nThe Desciption is %s%nThe quantity %d%nThe Price is %.1f%nThe Invoice Amount is %.1f%n==========================%n",invoice2.getPartNumber(),invoice2.getPartDescript ion(),invoice2.getQuantity(),invoice2.getPricePerItem(),invoice2.getInvoiceAmount()); invoice2.setQuantity(3); System.out.printf("The Invoice %s%nThe Desciption is %s%nThe quantity %d%nThe Price is %.1f%nThe Invoice Amount is %.1f%n=========================%n",invoice2.getPartNumber(),invoice2.getPartDescriptio n(),invoice2.getQuantity(),invoice2.getPricePerItem(),invoice2.getInvoiceAmount()); } } Question Three Pg. 11 Learning Outcome(s): Question Three 1.5 Marks Write a complete Java program that do the following: Explain the basic principles of programming, concept of language, and universal constructs of programming languages. a. Read your student’s ID number, store it in an array named myStudentNO and print it. b. find the number of even and odd integers in myStudentNO array Note: Your program output should look as shown below Develop a program based on specification using programming language elements including syntax, data types, conditional statement, control structures, procedures, arrays, objects and Note: you should Include the screenshot of the program output as a part of your answer and you should enter your student id number. Otherwise zero marks will be awarded. Question Three Pg. 12 Answer 1 package javaassi2q3; 2 import java.util.Scanner; 3 public class JavaAssi2Q3 { 4 public static void main(String[] args) { 5 int evenNumbers = 0; 6 int oddNumbers = 0; 7 int myStudentNO[] = new int[9]; 8 Scanner sc = new Scanner(System.in); 9 System.out.println("enter your Student's ID number"); 10 for (int i = 0; i < myStudentNO.length; i++) { 11 myStudentNO[i] = sc.nextInt(); 12 } 13 System.out.print("your student's number is "); 14 for (int i = 0; i < myStudentNO.length; i++) { 15 System.out.print(myStudentNO[i]); 16 if (myStudentNO[i] % 2 == 0) { 17 evenNumbers++; 18 } else { 19 oddNumbers++; 20 } 21 } 22 System.out.println(""); 23 System.out.println("Number of even numbers is:"+evenNumbers); 24 System.out.println("Number of odd numbers is:"+oddNumbers); 25 } 26 27 } Purchase answer to see full attachment

  
error: Content is protected !!