Question

# NAME MONDAY TUESDAY WEDNESDAY THURSDAY Kwame 20 25 25 30 Adwoa 10 10 40 35 Ama...

NAME MONDAY TUESDAY WEDNESDAY THURSDAY

Kwame 20 25 25 30

Ama 30 32 32 42

TOTAL

The table above represents the sales of personnel’s in a company for a period of four days. As a programmer write a Java program to populate all sales as shown in the tables above and calculate the total sales made in each day by the sales personals.

b. Using code snippets briefly explain the difference between Implicit casting (type widening) and Explicit casting (type narrowing) in java programming.

solution:

a.

Java program:

public class Main
{
public static void main(String[] args)
{
int i, j, days = 4, salesPersonals = 3;
int sales[][] = {{20, 25, 25, 30}, {10, 10, 40, 35}, {30, 32, 32, 42}}; //sales data
int totalSales[] = new int[days]; //a new array to store total sales on each day

for(i=0; i<days; i++) //lpop over all the days
{
for(j=0; j<salesPersonals; j++) //loop over all the sales personals
{
totalSales[i] += sales[j][i]; //add sales of each sales personals
}
//uncomment below to print total sales for each day
//System.out.println(totalSales[i]);
}
}
}

Screenshot:

b.

Difference
 Implicit Casting Explicit Casting Definition: Implicit type casting is the automatic conversion of a value of a particular data type into another data type. Explicit type casting is the conversion of a value of a particular data type into another when explicitly specified by the user. Example: //int 10 into double 10.0 double a = 10; //double 10.2 to int 10 int a = (int)10.2; Primitive Types: It is also known as widening because a value of smaller primitive type is converted into bigger primitive types. Eg: int to long, int to double, etc. It is also known as narrowing because a value in a bigger primitive type is converted into smaller primitive types by the user explicitly. Eg: explicit conversion from double to int. Subclass and Superclass: A subclass object reference can get converted into its superclass automatically. Eg: class A {....} class B extends A {....} ..... main(String args[]) { B b = new B(); A a = b; //widening } A superclass object reference is converted into its subclass explicitly by the user. Eg: class A {....} class B extends A {....} ..... main(String args[]) { A a = new B(); B b = (B)a; //narrowing }

#### Earn Coins

Coins can be redeemed for fabulous gifts.