dosyadan girdi alip BMI Hesaplama Yardim 3 Yıl, 11 Ay önce
|
Karma: 0
|
Kod: |
45;80;1.72;AUSTRIA
45;65;1.9;GERMANY
32;45.2;1.58;BELGIUM
55;50.5;1.72;AUSTRIA
65;85;1.98;BELGIUM
56;85;1.92;AUSTRIA
|
sonuc:
Kod: |
Name=AUSTRIA AverageAge=0 AverageWeight=0.0 Averageheight=0.0 AverageBMI=0.0
Name=BELGIUM AverageAge=0 AverageWeight=0.0 Averageheight=0.0 AverageBMI=0.0
Name=GERMANY AverageAge=0 AverageWeight=0.0 Averageheight=0.0 AverageBMI=0.0
|
Kod: |
public class Person {
public Person() {
super();
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getCountry() {
return country;
}
public void setCountry(String name) {
this.country = name;
}
public double getBmi() {
return weight / (Math.pow(height, 2));
}
public float getHeight() {
return height;
}
public void setHeight(float height) {
this.height = height;
}
public float getWeight() {
return weight;
}
public void setWeight(float weight) {
this.weight = weight;
}
private int age;
private String country;
private float height;
private float weight;
}
|
Kod: |
public class Country implements Comparable<Country> {
// Define a private String object name to store the name
private String name;
// Define a private int variable age to store the age
private int age ;
// Define a private float variable weight to store the weight
private float weight;
// Define a private float variable height to store the height
private float height;
// Define a private double variable bmi to store the bmi
private double bmi;
// Define a private int variable count to count the persons
private int count;
// Implement setter and getter for name
public String getName() {
return name;
}
public void setName(String name) {
this.name=name;
}
// Implement a public getAverageAge function, which returns the float (float)age/count
public float getAverageAge() {
return age/count;
}
// Implement a private addAge function with parameter int age, which adds the parameter to the age variable
private void addAge(int age){
this.age += age;
}
// Implement a public getAverageWeight function, which returns the float weight/count
public float getAverageWeight(){
return weight/count;
}
// Implement a private addWeight function with parameter float weight, which adds the parameter to the weight variable
private void addWeight(float weight){
this.weight = weight;
}
// Implement a public getAverageHeight function, which returns the float height/count
public float getAverageHeight(){
return height/count;
}
// Implement a private addHeight function with parameter float height, which adds the parameter to the height variable
private void addHeight(float height){
this.height=height;
}
// Implement a public getAverageBMI function, which returns the double bmi/count
public double getAverageBMI(){
return bmi/count;
}
// Implement a private addBMI function with parameter double bmi, which adds the parameter to the bmi variable
private void addBMI(double bmi){
this.bmi=bmi;
}
// Implement a public getCount getter for the count variable
public int getCount(){
return count;
}
// Implement a private addCount function with parameter int count, which adds the parameter to the count variable
private void addCount(int count){
this.count=count+1;
}
// Implement a public addPerson function with parameter Person p, which calls the addAge, addHeight, add Weight, addBMI and addCount functions
public void addPerson(Person p){
addAge(age);
addHeight(height);
addWeight(weight);
addBMI(bmi);
addCount(count);
}
// Implement a public toString function, which returns a String formatted the following way: name;averageage;averageweight;averageheight;averagebmi
public String toString(){
return "Name=" + name + " " + "AverageAge=" + age + " " + "AverageWeight=" + weight + " " + "Averageheight=" + height + " " + "AverageBMI=" + bmi;
}
// This function allows sorting of countries according to the count variable
@Override
public int compareTo(Country arg0) {
return arg0.count - this.count;
}
}
|
Kod: |
import java.io.*;
import java.util.*;
public class BMI_CSV {
public static void main(String[] args) {
try {
// Define and initialize an ArrayList persons to store Person objects
ArrayList<Person> persons = new ArrayList<Person>();
// Define and initialize an ArrayList country to store Country objects
ArrayList<Country> country = new ArrayList<Country>();
// Define a File object file and initialize it using the new File(args[0]) constructor
File file = new File(args[0]);
// Define a Scanner object inputStream and initialize it using the new Scanner(file) constructor
Scanner inputStream = new Scanner(file);
// Implement a while loop using inputStream.hasNextLine()
while(inputStream.hasNextLine())
{
// Define a String object line and initialize it using inputStream.nextLine()
String line = inputStream.nextLine();
// Define a String[] array fields and initialize it using line.split(";")
String[] fields = line.split(";");
// Create an empty Person object
Person person = new Person();
// Set the age of the person to Integer.valueOf(fields[0])
person.setAge(Integer.valueOf(fields[0]));
// Set the weight of the person to Float.valueOf(fields[1])
person.setWeight (Float.valueOf(fields[1]));
// Set the Height of the person to Float.valueOf(fields[2])
person.setHeight (Float.valueOf(fields[2]));
// Set the Country of the person to fields[3]
person.setCountry (fields[3]);
// Add the person object to the ArrayList persons
persons.add(person);
}
// Close the inputstream
inputStream.close();
// Implement a foreach loop using the persons ArrayList
for(Person Person : persons)
{
// Define a boolean variable added and initialize it with the value false
boolean added = false;
// Implement a foreach loop using the country ArrayList
for (Country Country : country)
{
// Implement an if condition with p.getCountry().equals(c.getName())
if(Person.getCountry().equals(Country.getName()))
{
// Add the person to the country Object
Country.addPerson(Person);
// Set added to true
added = true;
// Break
break;
}
}
// Implement an if condition using !added
if(!added)
{
// Create a new Country object
Country Country = new Country();
// Set the name of the country object to p.getCountry()
Country.setName (Person.getCountry());
// Call addPerson of the country object with the parameter person
Country.addPerson(Person);
// Add the country object to the ArrayList country
country.add(Country);
}
}
// Use Collections.sort to sort the country ArrayList
Collections.sort(country);
// Create a new PrintWriter object writer and initialize it using (args[1], "UTF-8")
PrintWriter writer = new PrintWriter(args[1], "UTF-8");
// Implement a foreach loop using the country ArrayList
for(Country Country: country)
{
// Use the println function of the writer using the country.toString function
writer.println(Country.toString());
// Print the same String to System.out
System.out.println(Country.toString());
}
// Close the writer
writer.close();
} catch (Exception e) {
System.out.println("Fehler bei der Verarbeitung.");
e.printStackTrace();
}
}
}
|
|
|
zamborna (Kullanıcı)
Yeni Üye
Gönderiler: 1
|