Hello!
I am having problem with my homework on methods and classes. I have started doing the first one, but I am pretty much lost.
Here are the two tasks I have for my homework:
1.) The weather man Coldy would like to manage information on his measurements. For every measurement he keeps information on time of measurement, location of a measurement and altitude. The measurement represents current temperature, humidity and barometric pressure. Besides that, he keeps writing his notes.
Write a class 'Measurement' to process information on measurements, that have been done. The program should contain two constructors. The first one should contain all parameters of the measurements, the second one should take the system time as the time of measurements, the remaining parameters should be defined by a user of a class.
Write the method, which allows the user to gain an access to all input or entered information (you can do it with get property). Write the methods, which allow a user to make notes on the measurements (you can do it with set property).
Write a method, which writes basic information on the measurement in a user-friendly way (you can change the standard method ToString).
2.) Write the main program, which will help Coldy to analyze the information on the measurements. For starters, the program should randomly 'generate' measurements according to the next algorhytm:
-for every city listed: Ljubljana(295-altitude), Kranj(386-altitude), Maribor(275-altitude), Koper(4-altitude), Celje(238-altitude)
-starts with today at 6:00 and continues with a period of 8 hours
-randomly chooses temperature between -20 and +40 degrees Celsius
-randomly chooses humidity between 20 and 100
-randomly chooses barometric pressure between 990 and 1030
Let the generated information write into a file. Let it write on the screen the name of the city, where it has rained most frequently (humidity>95), the city with a maximum and minimum average temperature and an average barometric pressure in all measurements.
Can you help me out? I am really lost in the first one and the second one is a big unknown to me.
Thank you for your answers!
Matjaz
Loading
MatjazPosted Apr 14, 2011, 4:37 PM
VulpesPosted Apr 14, 2011, 11:03 AM
Suthish NairPosted Apr 14, 2011, 10:13 AM
VulpesPosted Apr 14, 2011, 9:29 AM
MatjazPosted Apr 14, 2011, 9:15 AM
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Measurement
{
private int CurrentTemperature;
private int Humidity;
private int Altitude;
private int BarometricPressure;
private string Location= null;
private string Notes;
public void WritecurrentTemperature()
{
Console.WriteLine("Insert current temperature:");
bool ok = int.TryParse(Console.ReadLine(), out CurrentTemperature);
}
public void Writehumidity()
{
Console.WriteLine("Insert humidity:");
bool ok = int.TryParse(Console.ReadLine(), out Humidity);
}
public void Writealtitude()
{
Console.WriteLine("Insert altitude:");
bool ok = int.TryParse(Console.ReadLine(), out Altitude);
}
public void WriteBarometricPressure()
{
Console.WriteLine("Insert barometric pressure:");
bool ok = int.TryParse(Console.ReadLine(), out BarometricPressure);
}
public void WriteLocation()
{
Console.WriteLine("Insert location:");
string Location = Console.ReadLine();
}
public void WriteNotes()
{
Console.WriteLine("Insert notes:");
string Notes = Console.ReadLine();
}
public int currentTemperature
{
get
{
return CurrentTemperature;
}
}
public int humidity
{
get
{
return Humidity;
}
}
public int altitude
{
get
{
return Altitude;
}
}
public int barometricPressure
{
get
{
return BarometricPressure;
}
}
public string location
{
get
{
return Location;
}
}
public string notes
{
get
{
return Notes;
}
set
{
Notes = value;
}
}
public override string ToString()
{
return "Results of measurements: temperature" + CurrentTemperature;
}
}
class Program
{
static void Main(string[] args)
{
DateTime time = DateTime.Now;
string format = "d.M.yyyy HH:mm:ss ";
Console.WriteLine(time.ToString(format));
Measurement u = new Measurement();
u.WritecurrentTemperature();
Measurement u1 = new Measurement();
u1.Writehumidity();
Measurement u2 = new Measurement();
u2.Writealtitude();
Measurement u3 = new Measurement();
u3.WriteBarometricPressure();
Measurement u4 = new Measurement();
u4.WriteLocation();
Measurement u5 = new Measurement();
u5.WriteNotes();
Console.WriteLine(u.ToString());
Console.WriteLine(u1.ToString());
Console.WriteLine(u2.ToString());
Console.WriteLine(u3.ToString());
Console.WriteLine(u4.ToString());
Console.WriteLine(u5.ToString());
Console.ReadKey();
}
}
}
I can't figure out, how to write others down.
VulpesPosted Apr 14, 2011, 9:11 AM
public override string ToString()
{
return String.Format("Results of measurements{0} {1}:\n\n", Location, Altitude)
+ String.Format("temperatura = {0}, vlažnost = {1}, zracni pritisk = {2}\n\n", CurrentTemperature, Humidity, BarometricPressure)
+ String.Format(Notes);
}
where the last line + String.Format(Notes); gives an exception if 'Notes' is null.
In my own attempt, I gave the 'notes' field a default value of an empty string which avoids this problem. Alternative solutions which also work are:
+ String.Format("{0}", Notes);
+ Notes;
Another problem is that you're creating a new Measurement object every time. Consequently, when you call ToString() on the last object all its variables are zero or null.
MatjazPosted Apr 14, 2011, 8:24 AM
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Measurement
{
private int CurrentTemperature;
private int Humidity;
private int Altitude;
private int BarometricPressure;
private string Location= null;
private string Notes;
public void WritecurrentTemperature()
{
Console.WriteLine("Insert current temperature:");
bool ok = int.TryParse(Console.ReadLine(), out CurrentTemperature);
}
public void Writehumidity()
{
Console.WriteLine("Insert humidity:");
bool ok = int.TryParse(Console.ReadLine(), out Humidity);
}
public void Writealtitude()
{
Console.WriteLine("Insert altitude:");
bool ok = int.TryParse(Console.ReadLine(), out Altitude);
}
public void WriteBarometricPressure()
{
Console.WriteLine("Insert barometric pressure:");
bool ok = int.TryParse(Console.ReadLine(), out BarometricPressure);
}
public void WriteLocation()
{
Console.WriteLine("Insert location:");
string Location = Console.ReadLine();
}
public void WriteNotes()
{
Console.WriteLine("Insert notes:");
string Notes = Console.ReadLine();
}
public int currentTemperature
{
get
{
return CurrentTemperature;
}
}
public int humidity
{
get
{
return Humidity;
}
}
public int altitude
{
get
{
return Altitude;
}
}
public int barometricPressure
{
get
{
return BarometricPressure;
}
}
public string location
{
get
{
return Location;
}
}
public string notes
{
get
{
return Notes;
}
set
{
Notes = value;
}
}
public override string ToString()
{
return String.Format("Results of measurements{0} {1}:\n\n", Location, Altitude)
+ String.Format("temperatura = {0}, vlažnost = {1}, zracni pritisk = {2}\n\n", CurrentTemperature, Humidity, BarometricPressure)
+ String.Format(Notes);
}
}
class Program
{
static void Main(string[] args)
{
DateTime time = DateTime.Now;
string format = "d.M.yyyy HH:mm:ss ";
Console.WriteLine(time.ToString(format));
Measurement u = new Measurement();
u.WritecurrentTemperature();
Measurement u1 = new Measurement();
u1.Writehumidity();
Measurement u2 = new Measurement();
u2.Writealtitude();
Measurement u3 = new Measurement();
u3.WriteBarometricPressure();
Measurement u4 = new Measurement();
u4.WriteLocation();
Measurement u5 = new Measurement();
u5.WriteNotes();
Measurement m = new Measurement();
Console.WriteLine(m.ToString());
Console.ReadKey();
}
}
}
Can't figure out what is wrong.
VulpesPosted Apr 14, 2011, 6:33 AM
public int GetCurrentTemperature()
{
return currentTemperature();
}
// and so on for the others
It would also be clearer if you gave your 'Write' methods names such as WriteCurrentTemperature, WriteHumidity etc. rather than just Write and Write2. I know it's more long-winded but there's always intellisense there to save you typing everything in full :)
I'd include a ToString() method to write down all the variables at once as I did in my own attempt.
MatjazPosted Apr 13, 2011, 5:27 PM
I have written a code, which enables me to insert data on humidity, current temperature, altitude and barometric pressure.
But I can't figure out, how can I write all these details down at the end if I am stuck with get, set and toString.
That's all I have made so far:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Measurement
{
private int CurrentTemperature;
private int Humidity;
private int Altitude;
private int BarometricPressure;
public void Write()
{
Console.WriteLine("Insert current temperature:");
bool ok = int.TryParse(Console.ReadLine(), out CurrentTemperature);
}
public void Write2()
{
Console.WriteLine("Insert humidity:");
bool ok = int.TryParse(Console.ReadLine(), out Humidity);
}
public void Write3()
{
Console.WriteLine("Insert altitude");
bool ok = int.TryParse(Console.ReadLine(), out Altitude);
}
public void Write4()
{
Console.WriteLine("Insert barometric pressure");
bool ok = int.TryParse(Console.ReadLine(), out BarometricPressure);
}
}
class Program
{
static void Main(string[] args)
{
DateTime time = DateTime.Now;
string format = "d.M.yyyy HH:mm:ss ";
Console.WriteLine(time.ToString(format));
Measurement u = new Measurement();
u.Write();
Measurement u1 = new Measurement();
u1.Write2();
Measurement u2 = new Measurement();
u2.Write3();
Measurement u3 = new Measurement();
u3.Write4();
Console.ReadKey();
}
}
}
VulpesPosted Apr 12, 2011, 10:30 AM
As we have it at the moment, the user has to pass all 6 parameters to the first constructor which makes sense to me as you've only been told to code property 'getters' to retrieve those values. You'd need 'setters' as well if the user were to set the properties individually.
MatjazPosted Apr 12, 2011, 10:09 AM
But I noticed I made a big error translating.
Write a class 'Measurement' to process information on measurements, that have been done. The program should contain two constructors. The first one should contain all parameters of the measurements, the second one should take the system time as the time of measurements, the remaining parameters should be defined by a user of a class.
I should have translated this one like The first one should allow the input of all parameters of the measuerment.
Sorry for my error :S I hope I didn't waste too much of your time.
VulpesPosted Apr 12, 2011, 9:15 AM
As well as the 3 parameters you'd already allowed for, you also need to cater for the time, location and altitude of measurement as well as to allow the user to write notes. For the second constructor, I've set the time to the current system time and then called the first constructor (using 'chaining') to initialize the properties. If you haven't yet covered chaining in your studies, then just set all the properties 'manually' as you did for the first constructor.
Mpholo LeboeaPosted Apr 12, 2011, 9:13 AM
1. It is considered a bad programming to declare instance variables as public.
Rather declare them as private and use properties for access modifiers.
2. Declare variables to hold the user inputs for both humidity and barometric pressure values in Main method of class MainClass.
MatjazPosted Apr 12, 2011, 7:42 AM
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Measurement
{
public int CurrentTemeprature;
public int Humidity;
public int BarometricPressure;
public Measurement(int t, int v, int p)
{
CurrentTemeprature = t;
Humidity = v;
BarometricPressure = p;
}
public int v()
{
return Humidity;
}
public int p()
{
return BarometricPressure;
}
}
class MainClass
{
public static void Main()
{
Console.WriteLine("Insert humidity:");
Console.WriteLine("Insert barometric pressure:");
Console.ReadKey();
}
}
}
I am still getting a problem and can't figure out why...
Mpholo LeboeaPosted Apr 12, 2011, 7:16 AM
I can recommend C# how to program book to help you to get started with writing OOP applications.
Sahil JaniPosted Apr 12, 2011, 6:31 AM
It is not possible to give you whole program overhere.
I am giving you one link. Just refer that link. you will find better example related to your problems and you will solve it by yours.
Just refer this link :-> http://www.java2s.com/Tutorial/CSharp/CatalogCSharp.htm