none
Array question RRS feed

  • Question

  • I was given this recently by our teacher and I am already floating please help

    Write and test a program that allows the user to calculate how many litres of paint
    they need to purchase in order to redecorate a room.
    The program will take in several inputs. The first of these is the number of walls to be
    painted. This should be stored as a whole number. The second of these is the number
    of coats of paint required. Once these have been entered, the program should create
    an array of a suitable size to store the size of each wall in metres squared. This will be
    a real number. The program should then prompt the user to enter in the length and
    height for each wall, use these values to calculate the size of each wall in metres
    squared, and store the result (the area of the wall) in the correct location of the array.
    Once all inputs have been entered, the Enter button should be disabled, and the
    Calculate button should be enabled. When this is pressed, the program should
    calculate the number of litres of paint required for each wall.
    1 litre of paint covers 10m2 with one coat of paint. Therefore to cover a wall with n
    coats of paint, the formula would be:
    Litres of paint required = (area of wall in metres squared / 10 )* n 

    The number of litres of paint required for each wall should be stored in a separate
    array. The program should then also calculate the total area of walls to be painted,
    and the total number of litres of paint required. The contents of both arrays should
    then be displayed, alongside the totals for each. An example layout for four walls
    requiring 2 coats of paint is shown below.
    Wall Area in m2

    Paint required (l)

    Wall 1 12 2.4
    Wall 2 11.4 2.28
    Wall 3 7.68 1.536
    Wall 4 7.68 1.536
    Total 38.76 7.752
     The input data should be validated. The number of walls should be between 4
    and 24. No number of walls outwith this range should be accepted. Wall
    heights should be between 1.8 metres and 6.0 metres. No wall heights outwith
    these values should be accepted. Wall lengths should be between 1.2 metres
    and 20.0 metres. No wall lengths outwith these values should be accepted.
     The number of coats of paint should be between 1 and 4 and should be
    selected from a list such as a drop down menu or radio buttons.
     If an invalid entry is attempted for number of walls, wall height or wall length, a
    message should be displayed informing the user that the entry will not be
    accepted and indicating the valid range of values for that attribute.
     The number of walls should be stored as an integer.
     The wall areas should be stored in an array of doubles.
     The amount of paint required for each wall should be stored in an array of
    doubles.

    Saturday, November 17, 2018 5:29 PM

Answers

  • Hi

    It seems to me that the contents of your first post explain exactly what is required, and is about as simple as it can be.

    Try this code out. It doesn't serve as a copy/paste for your submission, nor does it follow the brief exactly but it is all I could come up with in a short time. It has limited error checks included.

    This is a stand alone example. Needs a Form1 with: NumericUpDown1 and NumericUpDown2 (both with Minimum Property set to 1), a couple of Labels as per Image, Button1 and Button2 as per Image and TextBox1 with multiline Property set to True.

    Build the Form1 in the Designer, and copy/replace all Form1 code with this code.

    Image

    Code

    Option Strict On
    Option Explicit On
    Imports System.Text
    Public Class Form1
      Dim Rooms As New List(Of Room)
      Dim PaintCoveragePerLitre As Double = 10
      Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        ' Get Inputs and store
        Dim newroom As New Room With {.PaintCoats = NumericUpDown2.Value}
    
        For i As Decimal = 1 To NumericUpDown1.Value
    
          Dim w As Double = GetDouble(InputBox("Enter Width for WALL " & i.ToString & " (meters)", "WALL WIDTH"))
          If w <= 0 Then Exit Sub
    
          Dim h As Double = GetDouble(InputBox("Enter Height for WALL " & i.ToString & " (meters)", "WALL HEIGHT"))
          If h <= 0 Then Exit Sub
    
          With newroom
            .Wall = i
            .PaintCoats = NumericUpDown2.Value
            .Walls.Add(New WallSize With {.WallWidth = w, .WallHeight = h, .WallArea = w * h})
          End With
        Next
        Rooms.Add(newroom)
        Button1.Enabled = False
        Button2.Enabled = True
      End Sub
      Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        ' Calculate
        For Each r As Room In Rooms
          TextBox1.Text = "Room " & r.Wall.ToString & vbCrLf
          For Each w As WallSize In r.Walls
            w.WallPaint = w.WallArea / PaintCoveragePerLitre * r.PaintCoats
            r.TotalPaint += w.WallPaint
            TextBox1.Text &= (w.WallWidth.ToString & "m x " & w.WallHeight.ToString & "m  " & w.WallArea.ToString("#.00m2  ") & w.WallPaint.ToString("0.00lt"))
            TextBox1.Text &= vbCrLf
          Next
          TextBox1.Text &= "Total Paint: " & r.TotalPaint.ToString("#.00ltrs") & vbCrLf
        Next
      End Sub
      Function GetDouble(s As String) As Double
        Dim v As Double = 0D
        If Double.TryParse(s, v) Then Return v
        Return 0D
      End Function
      Class Room
        Property Wall As Decimal
        Property Walls As New List(Of WallSize)
        Property PaintCoats As Decimal
        Property TotalPaint As Double
      End Class
      Class WallSize
        Property WallWidth As Double
        Property WallHeight As Double
        Property WallArea As Double
        Property WallPaint As Double
      End Class
    End Class


    Regards Les, Livingston, Scotland


    • Edited by leshay Sunday, November 18, 2018 2:43 PM
    • Marked as answer by Harshali Jagtap Monday, November 19, 2018 8:01 AM
    Sunday, November 18, 2018 2:42 PM

All replies

  • Hi

    Are you under the misapprehension that this forum serves as a school project writing service?

    *

    You will get ALL the help you need for YOUR code and issues concerning YOUR code.

    *

    So, you will need to post your code (in a code block), and details of any exceptions you are getting with the line(s) causing the error and the exact error message(s).


    Regards Les, Livingston, Scotland


    • Edited by leshay Saturday, November 17, 2018 6:07 PM
    Saturday, November 17, 2018 5:58 PM
  • hello, i havent yet started to code.
    Saturday, November 17, 2018 6:11 PM
  • Hi

    So are you expecting others here to write it for you?


    Regards Les, Livingston, Scotland

    Saturday, November 17, 2018 7:26 PM
  • hi,

    i never said that.

    I just want someone to explain it in a simpler way.
    Sunday, November 18, 2018 1:42 PM
  • Hi

    It seems to me that the contents of your first post explain exactly what is required, and is about as simple as it can be.

    Try this code out. It doesn't serve as a copy/paste for your submission, nor does it follow the brief exactly but it is all I could come up with in a short time. It has limited error checks included.

    This is a stand alone example. Needs a Form1 with: NumericUpDown1 and NumericUpDown2 (both with Minimum Property set to 1), a couple of Labels as per Image, Button1 and Button2 as per Image and TextBox1 with multiline Property set to True.

    Build the Form1 in the Designer, and copy/replace all Form1 code with this code.

    Image

    Code

    Option Strict On
    Option Explicit On
    Imports System.Text
    Public Class Form1
      Dim Rooms As New List(Of Room)
      Dim PaintCoveragePerLitre As Double = 10
      Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        ' Get Inputs and store
        Dim newroom As New Room With {.PaintCoats = NumericUpDown2.Value}
    
        For i As Decimal = 1 To NumericUpDown1.Value
    
          Dim w As Double = GetDouble(InputBox("Enter Width for WALL " & i.ToString & " (meters)", "WALL WIDTH"))
          If w <= 0 Then Exit Sub
    
          Dim h As Double = GetDouble(InputBox("Enter Height for WALL " & i.ToString & " (meters)", "WALL HEIGHT"))
          If h <= 0 Then Exit Sub
    
          With newroom
            .Wall = i
            .PaintCoats = NumericUpDown2.Value
            .Walls.Add(New WallSize With {.WallWidth = w, .WallHeight = h, .WallArea = w * h})
          End With
        Next
        Rooms.Add(newroom)
        Button1.Enabled = False
        Button2.Enabled = True
      End Sub
      Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        ' Calculate
        For Each r As Room In Rooms
          TextBox1.Text = "Room " & r.Wall.ToString & vbCrLf
          For Each w As WallSize In r.Walls
            w.WallPaint = w.WallArea / PaintCoveragePerLitre * r.PaintCoats
            r.TotalPaint += w.WallPaint
            TextBox1.Text &= (w.WallWidth.ToString & "m x " & w.WallHeight.ToString & "m  " & w.WallArea.ToString("#.00m2  ") & w.WallPaint.ToString("0.00lt"))
            TextBox1.Text &= vbCrLf
          Next
          TextBox1.Text &= "Total Paint: " & r.TotalPaint.ToString("#.00ltrs") & vbCrLf
        Next
      End Sub
      Function GetDouble(s As String) As Double
        Dim v As Double = 0D
        If Double.TryParse(s, v) Then Return v
        Return 0D
      End Function
      Class Room
        Property Wall As Decimal
        Property Walls As New List(Of WallSize)
        Property PaintCoats As Decimal
        Property TotalPaint As Double
      End Class
      Class WallSize
        Property WallWidth As Double
        Property WallHeight As Double
        Property WallArea As Double
        Property WallPaint As Double
      End Class
    End Class


    Regards Les, Livingston, Scotland


    • Edited by leshay Sunday, November 18, 2018 2:43 PM
    • Marked as answer by Harshali Jagtap Monday, November 19, 2018 8:01 AM
    Sunday, November 18, 2018 2:42 PM
  • hi,

    i never said that.

    I just want someone to explain it in a simpler way.

    The person that should do the explaining is your teacher.
    Sunday, November 18, 2018 2:43 PM
  • Hello,

    Let me try out code and thank you so much for your help, Sir.

    Regards

    Monday, November 19, 2018 8:02 AM