0% found this document useful (0 votes)
53 views34 pages

Lab Report-1

the lab report on the hooks law
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)
53 views34 pages

Lab Report-1

the lab report on the hooks law
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/ 34

Computer Science II

Lab Report # 1

User Name:Shahad aloraifej

User Number:202100060

Lab01_Java_Basics (5 points)

Write a function in Java that takes in two integers as parameters and returns the power.

Ex: 2,3 are parameters then the result would be 8 (23)

Write a function in Java that takes as parameters the number of hours a student registered, the
price of an hour, additional fees and then calculates the total amount that that student needs to
pay for that p articular semester.

Write a Java program that calculates the total amount of money you will have in your account
after 5 years if the original amount of money is 3000 and the interest rate is 3% per year.

import java.util.Scanner;

public class Main {

public static void main (String [] args){

Username checker = new Username();

System.out.println("Enter your First name");

Scanner p = new Scanner(System.in);

String first, last;

first = p.nextLine();

System.out.println("Enter your last name");

last = p.nextLine();

1
System.out.println("Enter your year of birth");

int year = p.nextInt();

checker.check(year, first, last);

class Username {

public void check(int year, String f1, String f2) {

System.out.print(f1 +" "+ f2+" " +" is born in "+ year);

if (2005 > year) {

System.out.print(" and he/she is qualified to drive a car");

else System.out.print(" and he/she isn't qualified to drive a car");

2
Lab02_Classes_And_Objects (15 points)

Task# 1

import java.util.Scanner;

public class Main {

public static void main (String [] args){

Username checker = new Username();

System.out.println("Enter the height");

Scanner p = new Scanner(System.in);

double height = p.nextInt();

System.out.println("Enter the base");

double base = p.nextInt();

3
double size = checker.check(height, base);

System.out.println("The area of the triangle is: "+ size);

class Username {

public double check(double base, double height) {

double size;

size = (base / 2) * height;

return size;

4
Task2#

/*

* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this


license

* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Main.java to edit this template

5
*/

package javaapplication89;

import java.util.Scanner;

public class Main {

public static void main (String [] args){

Circle tool = new Circle();

Scanner p = new Scanner(System.in);

System.out.println("Enter the radius");

tool.setRadius(p.nextDouble());

tool.display();

class Circle {

private double radius;

private double area;

private double volume;

public double getRadius() {

return radius;

public double getArea() {

6
area = Math.PI * Math.pow(radius, 2);

return area;

public double getVolume() {

volume = 4.0/3*Math.PI*Math.pow(radius,3);

return volume;

public Circle() {

radius = 0.0;

public Circle(double y){

radius = y;

public void setRadius(double radius) {

this.radius = radius;

public void display(){

System.out.println("The radius is: "+getRadius());

System.out.println("The area is: "+getArea());

System.out.println("The volume is: "+getVolume());

7
}

Task# 3

/*

* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this


license

* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Main.java to edit this template

*/

package javaapplication606;

public class Main {

8
public static void main (String [] args){

Invoice tool = new Invoice();

Invoice checker = new Invoice();

Scanner p = new Scanner(System.in);

System.out.println("Enter the part number");

tool.setpartnum(p.nextLine());

System.out.println("Enter the description of the unit");

tool.setpartdesc(p.nextLine());

System.out.println("Enter the quantity");

tool.setquantity(p.nextInt());

System.out.println("Enter the price of item");

tool.setPrice(p.nextInt());

System.out.println("Part number: "+tool.getpartnum());

System.out.println("Details: "+tool.getpartdesc());

System.out.println("The quantity: "+tool.getquantity());

System.out.println("Price per piece: "+tool.getPrice());

System.out.println("The total price is: "+tool.getInvoiceAmount());

class Invoice {

private String pnum, pdesc;

private int num;

private double price;

9
public void setpartnum(String x) {

pnum = x;

public String getpartnum() {

return pnum;

public void setpartdesc(String y) {

pdesc = y;

public String getpartdesc() {

return pdesc;

public void setquantity(int n) {

num = n;

public int getquantity() {

return num;

10
}

public void setPrice(double p) {

price = p;

public double getPrice() {

return price;

public double getInvoiceAmount(){

double Value = num * price;

if ( Value > 0){

return Value;

else return 0;

Task# 4

11
/*

* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this


license

* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Main.java to edit this template

*/

package javaapplication89;

public class Main {

public static void main (String [] args){

Petrolpurchase tool = new Petrolpurchase();

System.out.println("Details before update");

System.out.println("Gas station location: "+tool.getStationLocation());

System.out.println("Petrol type: "+tool.getPetrolType());

System.out.println("Quantity: "+tool.getQuantity());

12
System.out.println("Price: "+tool.getPrice());

System.out.println("Discount: "+ tool.getDiscount());

System.out.println("Net amount: "+tool.getPurchaseAmount());

System.out.println();

System.out.println();

tool.setStationLocation("Khobar");

tool.setPetrolType("Super Premium");

tool.setQuantity(10);

tool.setPrice(3.15);

tool.setDiscount(0.05);

System.out.println("Details after update");

System.out.println("Gas station location: "+tool.getStationLocation());

System.out.println("Petrol type: "+tool.getPetrolType());

System.out.println("Quantity: "+tool.getQuantity());

System.out.println("Price: "+tool.getPrice());

System.out.println("Discount: "+ tool.getDiscount());

System.out.println("Net amount: "+tool.getPurchaseAmount());

class Petrolpurchase {

private String StationLocation = "Riyadh", PetrolType = "Premium";

private int Quantity = 15;

13
private double price=2.03, discount=0.10;

public void setStationLocation(String x) {

StationLocation = x;

public String getStationLocation() {

return StationLocation;

public void setPetrolType(String y) {

PetrolType = y;

public String getPetrolType() {

return PetrolType;

public void setQuantity(int n) {

Quantity = n;

public int getQuantity() {

14
return Quantity;

public void setPrice(double p) {

price = p;

public double getPrice() {

return price;

public void setDiscount(double d){

discount = d;

public double getDiscount(){

return discount;

public double getPurchaseAmount(){

double purchaseAmount = Quantity * price;

double discountAmount = purchaseAmount * discount;

return purchaseAmount - discountAmount;

15
}

Lab03_Overloaded_Constructor_Accessor_Mutator (15 points)

Task# 1

/*

* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this


license

* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Main.java to edit this template

*/

package javaapplication89;

public class Main {

public static void main (String [] args){

Date tool = new Date();

16
System.out.println("The initial date is: "+tool.getDay()+"/"+tool.getMonth()
+"/"+tool.getYear());

tool.setDay(11);

tool.setMonth(1);

tool.setYear(2003);

System.out.println("Date with new values is: "+tool.getDay()+"/"+tool.getMonth()


+"/"+tool.getYear());

class Date {

private int month = 4;

private int day = 7;

private int Year = 2004;

public int getMonth() {

return month;

public void setMonth(int month) {

this.month = month;

public int getDay() {

return day;

17
}

public void setDay(int day) {

this.day = day;

public int getYear() {

return Year;

public void setYear(int year) {

Year = year;

18
Task# 2

/*

* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this


license

* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Main.java to edit this template

*/

package javaapplication89;

import java.util.Scanner;

public class Main {

public static void main (String [] args){

Circle tool = new Circle();

Scanner p = new Scanner(System.in);

System.out.println("Enter the radius");

tool.setRadius(p.nextDouble());

19
tool.display();

class Circle {

private double radius;

private double area;

private double volume;

public double getRadius() {

return radius;

public double getArea() {

area = Math.PI * Math.pow(radius, 2);

return area;

public double getVolume() {

volume = 4.0/3*Math.PI*Math.pow(radius,3);

return volume;

public Circle() {

radius = 0.0;

20
}

public Circle(double y){

radius = y;

public void setRadius(double radius) {

this.radius = radius;

public void display(){

System.out.println("The radius is: "+getRadius());

System.out.println("The area is: "+getArea());

System.out.println("The volume is: "+getVolume());

21
Task# 3

/*

* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this


license

* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Main.java to edit this template

*/

package javaapplication89;

public class Main {

public static void main (String [] args){

Car tool1 = new Car();

Car tool2 = new Car();

tool1.setModel("Mazda");

22
tool1.setYear("2001");

tool1.setPrice(25000.0);

tool2.setModel("Toyota");

tool2.setYear("2005");

tool2.setPrice(50000.0);

System.out.println("These are the details before the discount");

tool1.display();

System.out.println();

tool2.display();

System.out.println();

System.out.println("These are the details after the discount");

tool1.display(0.05);

System.out.println();

tool2.display(0.07);

class Car {

public String getModel() {

return model;

public void setModel(String model) {

23
this.model = model;

public String getYear() {

return year;

public void setYear(String year) {

this.year = year;

public double getPrice() {

return price;

public void setPrice(double price) {

if (price > 0) {

this.price = price;

else System.out.println("the price is negative and it didn't register");

private String model, year;

24
private double price;

public Car(){

model = "Toyota";

year = "2005";

price = 25000.0;

public void display(){

System.out.println("Model: "+getModel());

System.out.println("Year: "+getYear());

System.out.println("Price "+getPrice());

public void display(double discount){

double dis, afterdis;

dis = getPrice() * discount;

afterdis = getPrice() - dis;

System.out.println("Model: "+getModel());

System.out.println("Year: "+getYear());

System.out.println("Price "+afterdis);

25
Lab04_Inheritance (15 points)

Exercise 1:

/*

* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license

* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Main.java to edit this template

*/

package javaapplication606;

public class Main {

public static void main(String[] args) {

weighedItem tool1 = new weighedItem("Banana", 3.00,1.37);

CountedItem tool2 = new CountedItem("Pens",4.5,10);

26
tool1.printmethod();

tool2.printmethod();

}// END OF Main Class

class PurchaseItem {

private String name;

private double unitPrice;

PurchaseItem(String name, double unitPrice) {

this.name = name;

this.unitPrice = unitPrice;

PurchaseItem(){

name = "no item";

unitPrice = 0;

public void setName(String name) {

this.name = name;

27
}

public void setUnitPrice(double unitPrice) {

this.unitPrice = unitPrice;

public String getName() {

return name;

public double getUnitPrice() {

return unitPrice;

public void printmethod() {

System.out.print(getName()+" @ "+getUnitPrice());

}//END OF Super Class

class weighedItem extends PurchaseItem{

private double weight;

28
public weighedItem(String name, double unitPrice, double weight){

super(name, unitPrice);

this.weight = weight;

@Override

public double getUnitPrice(){

return super.getUnitPrice() * weight;

@Override

public void printmethod(){

super.printmethod();

System.out.println(" "+weight+"Kg "+getUnitPrice()+"SR");

}// END OF CLASS WeightedItem

class CountedItem extends PurchaseItem {

private int quantity;

public CountedItem(String name, double unitPrice, int quantity) {

super(name, unitPrice);

this.quantity = quantity;

29
@Override

public double getUnitPrice() {

return super.getUnitPrice() * quantity;

@Override

public void printmethod(){

super.printmethod();

System.out.println(" "+quantity+"Unit "+getUnitPrice()+"SR");

}//END OF CLASS CountedItem

Exercise 2:

30
/*

* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this


license

* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Main.java to edit this template

*/

package javaapplication89;

public class Main {

public static void main(String[] args) {

PaperBook tool = new PaperBook("Absolute Java","ABC Author",2017,"Pearson


Publication","B1243343-34234");

System.out.println("Paper Book Details");

System.out.println();

31
System.out.println(tool.toString());

System.out.println();

System.out.println();

AudioBook tool2 = new AudioBook("Absolute Java","XYZ Author",2017,150,90,"Peter


Park");

System.out.println("Audio Book Details");

System.out.println();

System.out.println(tool2.toString());

System.out.println();

class Book{

private String name, author;

private int publish;

public Book(String name, String author, int publish){

32
this.name = name;

this.author = author;

this.publish = publish;

@Override

public String toString(){

return name+" - "+author+" - "+publish;

class AudioBook extends Book {

private int size, length;

private String artistName;

public AudioBook(String name, String author, int publish, int size, int length, String
artistName) {

super(name,author,publish);

this.artistName = artistName;

this.length = length;

this.size = size;

@Override

public String toString(){

return super.toString() + " - " + size + " - "+length+" - "+artistName;

33
}

class PaperBook extends Book{

private String publisher, isbn;

public PaperBook(String name, String author, int publish, String publisher, String isbn){

super(name, author, publish);

this.publisher = publisher;

this.isbn = isbn;

@Override

public String toString(){

return super.toString() + " - " + publisher + " - "+isbn;

34

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy