Question Index

Write a program to calculate the weight fractions in an alloy given the weights for individual metals. Note that the fractions should add up to one.

Input (Map) Output (Map)
{Iron=92.0, Zinc=100.0, Calcium=36.9, Chromium=189.20, Nickel=29.0, Cobalt=102.0} {Iron=0.16755, Zinc=0.18212, Calcium=0.0672, Chromium=0.34456, Nickel=0.05281, Cobalt=0.18576}
{Iridium=12.0, Platinum=16.20, Silver=192.0, Thorium=99.99, Lead=92.62} {Iridium=0.02907, Platinum=0.03924, Silver=0.46511, Thorium=0.34456, Lead=0.22436}
{Copper=9.0, Potassium=16.9, Magnesium=36.2, Mercury=129.20, Nobelium=92.0, Terbium=102.0, Cerium=2.0} {Copper=0.02324, Potassium=0.04364, Magnesium=0.09347, Mercury=0.33359, Nobelium=0.23754, Terbium=0.26336, Cerium=0.00516}
{Thorium=0.2, Lead=0.9, Silver=12.02, Chromium=16.0, Radium=92.0, Germanium=99.0} {Thorium=0.00091, Lead=0.00409, Silver=0.05461, Chromium=0.07269, Radium=0.41795, Germanium=0.44975}

import java.util.*;
class CalculateWeightFractions
    public static void main(String s[])
        Map<String, Double> weights = new HashMap<String, Double>();
        weights.put("Iron", 92.0);
        weights.put("Zinc", 100.0);
        weights.put("Calcium", 36.9);
        weights.put("Chromium", 189.20);
        weights.put("Nickel", 29.0);
        weights.put("Cobalt", 102.0);
        System.out.println("The fractions of given elements are : " + getWeightFractions(weights));


    public static Map<String, Double> getWeightFractions(Map<String, Double> weights) {




Topic: Java HashMap Implementation

Read this topic
Take test on this topic

Score more than 2 points

© meritcampus 2019

All Rights Reserved.

Open In App