0% found this document useful (0 votes)
49 views10 pages

CS506 assignment 1 2025 solution

The document outlines an assignment for a web design and development course, specifically focusing on a Java program for sales prediction based on temperature data. The program collects user input for temperature and ice cream sales, trains a predictive model using linear regression, and allows users to make predictions. The assignment is due on May 2, 2025, and is worth a total of 20 marks.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
49 views10 pages

CS506 assignment 1 2025 solution

The document outlines an assignment for a web design and development course, specifically focusing on a Java program for sales prediction based on temperature data. The program collects user input for temperature and ice cream sales, trains a predictive model using linear regression, and allows users to make predictions. The assignment is due on May 2, 2025, and is worth a total of 20 marks.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

CS506 – Web Design and Development

Assignment No. 01 Total Marks: 20


Semester: Spring 2025 Due Date: 2-May-2025
For Paid Assignment 03234131842

Question No 1

Solution
Code
import javax.swing.*;

import java.util.ArrayList;

public class SalesPrediction {

private double slope;

private double intercept;

public static void main(String[] args) {

new SalesPrediction().init();

private void init() {

int numPoints = 0;

while (true) {

try {

All Subject Paid Assignment Available 03234131842 Page 1


String input = JOptionPane.showInputDialog(null, "Enter number of data points (2 to 50):");

if (input == null) return; // cancel case

numPoints = Integer.parseInt(input);

if (numPoints >= 2 && numPoints <= 50) break;

else JOptionPane.showMessageDialog(null, "Please enter a number between 2 and 50.");

} catch (NumberFormatException e) {

JOptionPane.showMessageDialog(null, "Invalid input! Please enter a valid number.");

ArrayList<DataPoint> data = collectDataSet(numPoints);

trainModel(data);

boolean keepPredicting = true;

while (keepPredicting) {

int temperature = 0;

while (true) {

try {

String input = JOptionPane.showInputDialog(null, "Enter temperature (°C) to predict ice


cream sales for [1–50]:");

if (input == null) return;

temperature = Integer.parseInt(input);

if (temperature >= 1 && temperature <= 50) break;

else JOptionPane.showMessageDialog(null, "Temperature must be between 1 and 50.");

} catch (NumberFormatException e) {

All Subject Paid Assignment Available 03234131842 Page 2


JOptionPane.showMessageDialog(null, "Invalid input! Please enter a valid integer.");

double prediction = predictSales(temperature);

String result = String.format(

"Model Parameters:\nSlope: %.2f\nIntercept: %.2f\n\nPredicted Sales for %d°C: %.2f units",

slope, intercept, temperature, prediction

);

JOptionPane.showMessageDialog(null, result);

int choice = JOptionPane.showConfirmDialog(null, "Would you like to predict another sales


value?", "Continue?", JOptionPane.YES_NO_OPTION);

if (choice != JOptionPane.YES_OPTION) {

JOptionPane.showMessageDialog(null, "Thank you! Goodbye!\nDeveloped by BC200417617");

keepPredicting = false;

private ArrayList<DataPoint> collectDataSet(int numOfPoints) {

ArrayList<DataPoint> data = new ArrayList<>();

for (int i = 1; i <= numOfPoints; i++) {

int temperature = 0;

float sales = 0;

All Subject Paid Assignment Available 03234131842 Page 3


boolean valid = false;

while (!valid) {

try {

String tempInput = JOptionPane.showInputDialog(null, "Data Point: " + i + "\nEnter


Temperature (°C) [1–50]:");

if (tempInput == null) System.exit(0);

temperature = Integer.parseInt(tempInput);

if (temperature < 1 || temperature > 50) {

JOptionPane.showMessageDialog(null, "Temperature must be between 1 and 50.");

continue;

String salesInput = JOptionPane.showInputDialog(null, "Data Point: " + i + "\nEnter Ice Cream


Sales (units) [1–500]:");

if (salesInput == null) System.exit(0);

sales = Float.parseFloat(salesInput);

if (sales < 1 || sales > 500) {

JOptionPane.showMessageDialog(null, "Sales must be between 1.0 and 500.0.");

continue;

valid = true;

data.add(new DataPoint(temperature, sales));

} catch (NumberFormatException e) {

JOptionPane.showMessageDialog(null, "Invalid input! Please enter valid numbers.");

All Subject Paid Assignment Available 03234131842 Page 4


}

return data;

public void trainModel(ArrayList<DataPoint> data) {

int n = data.size();

double sumX = 0, sumY = 0, sumXY = 0, sumX2 = 0;

for (DataPoint dp : data) {

int x = dp.getTemperature();

float y = dp.getSales();

sumX += x;

sumY += y;

sumXY += x * y;

sumX2 += x * x;

slope = (n * sumXY - sumX * sumY) / (n * sumX2 - sumX * sumX);

intercept = (sumY - slope * sumX) / n;

private double predictSales(int temperature) {

All Subject Paid Assignment Available 03234131842 Page 5


return slope * temperature + intercept;

// Inner class DataPoint

private static class DataPoint {

private int temperature;

private float sales;

public DataPoint() {

this.temperature = 0;

this.sales = 0.0F;

public DataPoint(int temperature, float sales) {

this.temperature = temperature;

this.sales = sales;

public DataPoint(DataPoint dp) {

this.temperature = dp.temperature;

this.sales = dp.sales;

public void setTemperature(int temperature) {

this.temperature = temperature;

All Subject Paid Assignment Available 03234131842 Page 6


}

public void setSales(float sales) {

this.sales = sales;

public int getTemperature() {

return temperature;

public float getSales() {

return sales;

Output ScreenShot

All Subject Paid Assignment Available 03234131842 Page 7


For Paid Assignment
03234131842

For Paid Assignment


03234131842

All Subject Paid Assignment Available 03234131842 Page 8


For Paid Assignment
03234131842

For Paid Assignment


03234131842

All Subject Paid Assignment Available 03234131842 Page 9


For Paid Assignment
03234131842

For Paid Assignment


03234131842

All Subject Paid Assignment Available 03234131842 Page 10

You might also like