Solution for Introduction to Programming Assignment - KOI ICT102

Verified

Added on  2022/11/17

|12
|1129
|91
Homework Assignment
AI Summary
This document presents a solution to an Introduction to Programming assignment (ICT102) from King's Own Institute (KOI). The assignment covers fundamental programming concepts using Java, including variable declaration, data types, arithmetic operations, user input using Scanner, array manipulation, and conditional statements. The solution demonstrates how to calculate stock transactions, analyze rainfall data, process sales figures for different departments, and search through arrays of strings. The provided Java code includes clear comments and demonstrates problem-solving approaches for each question, providing a comprehensive understanding of the assignment requirements and programming logic.
Document Page
Running head: INTRODUCTION TO PROGRAMMING
Introduction to programming
Name of the Student
Name of the University
Authors note
tabler-icon-diamond-filled.svg

Paraphrase This Document

Need a fresh take? Get an instant paraphrase of this document with our AI Paraphraser
Document Page
1INTRODUCTION TO PROGRAMMING
Q1.
public class Question1 {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
int sharenum = 1000;
//number of shares
double btstockprice = 32.87;
//buy price of stocks
double commrate = .02;
//commission rate
double sstockprice = 33.92;
double buyamnt = (sharenum * btstockprice);
double buycomm = ((sharenum * btstockprice) * commrate);
double salamnt = (sharenum * sstockprice);
double salcomm = ((sharenum * sstockprice) * commrate);
Document Page
2INTRODUCTION TO PROGRAMMING
System.out.println(" Total amount paid by Joe for purchsing Stock \t $" +
buyamnt);
System.out.println(" Total amount paid by for Brockers Commision \t $" +
buycomm);
System.out.println(" Total amount acquired by Joe by selling the Stock \t $" +
salamnt);
System.out.println(" Total amount paid to broker after sales of stocks \t $" +
salcomm);
System.out.println(" Total profit amount after the sale of Stocks \t $" +
(salamnt - buyamnt));
}
}
Document Page
3INTRODUCTION TO PROGRAMMING
Q2.
public class Q2 {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
int numyears;
int months = 12;
double[] rainfall = new double[12];
tabler-icon-diamond-filled.svg

Paraphrase This Document

Need a fresh take? Get an instant paraphrase of this document with our AI Paraphraser
Document Page
4INTRODUCTION TO PROGRAMMING
double sum = 0;
double average = 0;
Scanner sc = new Scanner(System.in);
System.out.println("Enter the Number of years");
numyears = sc.nextInt();
for (int o = 1; o <= numyears; o++) {
for (int y = 0; y < months; y++) {
//double sum = 0;
System.out.println(" Enter the rainfall for month " + (y + 1));
rainfall[y] = sc.nextDouble(); //taking input from the user
sum = sum + rainfall[y];
//Calculating the sum
System.out.println(sum);
average = sum / months;
//Calculating the average
}
Document Page
5INTRODUCTION TO PROGRAMMING
System.out.println("Average for the year " + numyears + " and average
rainfall is " + average);
sum = 0;
}
}
}
Document Page
6INTRODUCTION TO PROGRAMMING
Q3
*/
public class Q3 {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
//double[][] salesarr = new double[6][4];
double sum1 = 0;
tabler-icon-diamond-filled.svg

Paraphrase This Document

Need a fresh take? Get an instant paraphrase of this document with our AI Paraphraser
Document Page
7INTRODUCTION TO PROGRAMMING
Scanner sc = new Scanner(System.in);
//System.out.print("Enter 2D array size for storing the Sales of the departments :
");
System.out.println("Enter the value of Number of Departments");
// Scanner sc=new Scanner(System.in);
int rows = sc.nextInt();
System.out.println("Enter the value of Number of Quarters");
int columns = sc.nextInt();
System.out.println("Enter the sales of the departments : ");
int twoD[][] = new int[rows][columns];
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
System.out.println("Enter the Sales for Department " + (i + 1) + " and for
qurater " + (j + 1));
twoD[i][j] = sc.nextInt();
//sum1 = sum1 + twoD[i][j];
Document Page
8INTRODUCTION TO PROGRAMMING
}
//System.out.println("");
//System.out.println("Total sales for department " + (i + 1) + " is" + sum1);
}
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
System.out.println("Sales of the departments " + (i + 1) + " and for qurater "
+ (j + 1) + " is " + twoD[i][j]);
//twoD[i][j] = sc.nextInt();
sum1 = sum1 + twoD[i][j];
}
//System.out.println("");
System.out.println("Total sales for department " + (i + 1) + " is" + sum1);
}
System.out.println("The Average sales of quarter is " + (sum1 / rows));
//f//or (int k = 1; k <= 6; k++) {
Document Page
9INTRODUCTION TO PROGRAMMING
// for (int l = 1; l <= 4; l++) {
//System.out.println(" The sales for division " + k + "is " + salesarr[k][l]);
//}
// }
}
}
Q4.
public class Q4 {
tabler-icon-diamond-filled.svg

Paraphrase This Document

Need a fresh take? Get an instant paraphrase of this document with our AI Paraphraser
Document Page
10INTRODUCTION TO PROGRAMMING
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
String[] firstName = {"Jhon", "Caly", "Justin", "Judith", "Daniel", "Trisha",
"Daniel"};
String[] phonenum = {"225-5241", "258-147", "963-852", "862-741", "987-
456", "745-965", "325-147", "865-245", "987-124", "258-964"};
System.out.println("Enter the initials you want to look for");
Scanner sc = new Scanner(System.in);
String inp = sc.next();
for (int i = 0; i < 10; i++) {
if (firstName[i].contains(inp))
//comparing the user input with all the available array elements
{
Checking the name array if the
System.out.print(firstName[i]);
Document Page
11INTRODUCTION TO PROGRAMMING
System.out.print("\t");
System.out.println(phonenum[i]);
}
}
}
}
chevron_up_icon
1 out of 12
circle_padding
hide_on_mobile
zoom_out_icon
[object Object]