## Problem 1: Products in a store

Consider the Product class below, which represents products in an e-commerce store. It is extended by Computer class, and there is also an interface for mobile devices:

``````    /** Represents any product on sale */
class Product {
String name;
double price;
Product(String name, double price){
this.name=name;
this.price=price;
}
}

/** Represents computer products */
class Computer extends Product {
String CPU;
int RAMGB;
Computer(String name, double price, String CPU, int RAMGB) {
super(name,price);
this.CPU=CPU;
this.RAMGB=RAMGB;
}
}

/** Represents mobile devices */
interface Mobile {
int getBatteryTime(); //in minutes
double getWeight(); //in kg
}``````

Write a `Laptop` class which extends Computer and implements the Mobile interface.

## Solution

``````    class Laptop extends Computer implements Mobile {
int batteryTime;
double weight;
LaptopB;
Computer(String name, double price, String CPU, int RAMGB,int batteryTime, double weight) {
super(name,price,CPU,RAMGB);
this.batteryTime=batteryTime;
this.weight=weight;
}
public int getBatteryTime() {return batteryTime;}
public double getWeight() {return weight;}
}``````

## Problem 2: Matrix class

A simplified version of the Matrix class from last week is given below. Modify the class to support (i.e. implement) the following interface:

``````    interface Multipliable {
/** returns multiplication of this matrix with the other matrix. Throws exception if sizes mismatch */
Matrix multiply(Matrix other);
}``````

Below is the Matrix class:

``````    class Matrix {
double[][] data;

Matrix(int rows, int columns) {
data=new double[rows][columns];
}

Matrix(double[][] data){
this.data=data;
}

int numRows() {return data.length;}

int numColumns() {return data[0].length;}

void setElement(int r, int c, double element) {
data[r][c]=element;
}

double getElement(int r, int c) {
return data[r][c];
}
}``````

## Solution

``````    class Matrix implements Multipliable {
...
Matrix multiply(Matrix other) throws Exception{
if(numColumns()!=other.numRows())
throw new Exception("Size mismatch");
Matrix retval=new Matrix(numRows(),other.numColumns();
for(int i=0_i<retval.numRows();i++)
for(int j=0;j<retval.numColumns();j++) {
double val=0;
for (int k=0;k<numColumns();k++)
val+=getElement(i,k)*other.getElement(k,j);
retval.setElement(i,j,val);
}
}
}``````