Guest User

Untitled

a guest
Feb 18th, 2019
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.26 KB | None | 0 0
  1. import java.util.Scanner;
  2. public class Menu {
  3.  
  4. static void displayMainMenu(){
  5. Scanner scan = new Scanner(System.in);
  6.  
  7. System.out.println("1.ConversionSubMenu");
  8. System.out.println("2.My1DArraySubMenu");
  9.  
  10. System.out.print("Select : ");
  11. String Ans = scan.next();
  12.  
  13. System.out.println("");
  14.  
  15. int n1 = Integer.parseInt(Ans);
  16.  
  17.  
  18. if(n1 ==1){
  19. displayConversionSubMenu();
  20. }else if(n1 ==2){
  21. displayMy1DArraySubMenu();
  22. }else{
  23. System.out.println("Error Selection Try Again");
  24. displayMainMenu();
  25. }
  26.  
  27.  
  28. }
  29.  
  30.  
  31.  
  32.  
  33. static void displayConversionSubMenu(){
  34.  
  35. Scanner scan = new Scanner(System.in);
  36.  
  37. System.out.println("1. Grams To ounces");
  38. System.out.println("2. Ounces To Grams");
  39. System.out.println("3. Kilograms to Stone");
  40. System.out.println("4. Stone to kilograms");
  41. System.out.println("5. Pounds to gram");
  42. System.out.println("6. Grams to pounds");
  43. System.out.println("0. Exit");
  44.  
  45. System.out.print("Select : ");
  46. String Ans = scan.next();
  47. System.out.println("");
  48.  
  49. int n1 = Integer.parseInt(Ans);
  50.  
  51. Conversion c1 = new Conversion();
  52.  
  53. Scanner scan1 = new Scanner(System.in);
  54.  
  55. if(n1 == 1){
  56. System.out.print("Input Weight : ");
  57. double w = scan.nextDouble();
  58. System.out.println("Answer is "+c1.GramsToOunces(w)+" Ounces");
  59.  
  60. }else if(n1==2){
  61. System.out.print("Input Weight : ");
  62. double w = scan.nextDouble();
  63. System.out.println("Answer is "+c1.OunToGrams(w)+"g");
  64.  
  65.  
  66. }else if(n1 == 3){
  67.  
  68. System.out.print("Input Weight : ");
  69. double w = scan.nextDouble();
  70. System.out.println("Answer is "+c1.KgToStone(w)+" Stones");
  71.  
  72.  
  73. }else if(n1 == 4){
  74. System.out.print("Input Weight : ");
  75. double w = scan.nextDouble();
  76. System.out.println("Answer is "+c1.StoneToKg(w)+"kg");
  77.  
  78.  
  79.  
  80. }else if(n1 == 5){
  81. System.out.print("Input Weight : ");
  82. double w = scan.nextDouble();
  83. System.out.println("Answer is "+c1.PoundsToGram(w)+"g");
  84.  
  85.  
  86.  
  87. }else if(n1 == 6){
  88. System.out.print("Input Weight : ");
  89. double w = scan.nextDouble();
  90. System.out.println("Answer is "+c1.GramToPound(w)+" Ounces");
  91.  
  92.  
  93.  
  94. }else if(n1 == 0){
  95. displayMainMenu();
  96.  
  97. }else{
  98. System.out.println("Wrong Input ,Try Again!");
  99. System.out.println("");
  100. displayConversionSubMenu();
  101. }
  102.  
  103. System.out.println("");
  104. displayConversionSubMenu();
  105. System.out.println("");
  106.  
  107. }
  108.  
  109.  
  110.  
  111.  
  112. static void displayMy1DArraySubMenu(){
  113.  
  114. Scanner scan = new Scanner(System.in);
  115.  
  116. System.out.println("1 Total");
  117. System.out.println("2 Average");
  118. System.out.println("3 Get Total??");
  119. System.out.println("0 Exit");
  120.  
  121. int a = scan.nextInt();
  122. System.out.println("");
  123.  
  124.  
  125. My1DArray m1 =new My1DArray();
  126.  
  127.  
  128. Scanner scan1 = new Scanner(System.in);
  129. if(a ==1){
  130. System.out.print("Input the size of the array :");
  131. int size = scan1.nextInt();
  132. System.out.println("");
  133.  
  134. System.out.print("Input the Elements :");
  135. System.out.println("");
  136. double[] array1 = new double[size];
  137. Scanner scan2 = new Scanner(System.in);
  138.  
  139. for(int i =0;i<size;i++){
  140. array1[i] = scan1.nextDouble();
  141. }
  142.  
  143. System.out.println("Total is "+m1.getTotal(array1));
  144.  
  145.  
  146. }else if(a==2){
  147. System.out.print("Input the size of the array :");
  148. int size = scan1.nextInt();
  149. System.out.println("");
  150.  
  151. System.out.print("Input the Elements :");
  152. System.out.println("");
  153. double[] array1 = new double[size];
  154. Scanner scan2 = new Scanner(System.in);
  155.  
  156. for(int i =0;i<size;i++){
  157. array1[i] = scan1.nextDouble();
  158. }
  159.  
  160. System.out.println("Total is "+m1.getAverage(array1));
  161.  
  162.  
  163. }else if(a==3){
  164.  
  165.  
  166. }else if(a==0){
  167. displayMainMenu();
  168. System.out.println("");
  169.  
  170.  
  171. }else{
  172. System.out.println("Wrong input Try Again!!");
  173. System.out.println("");
  174. displayMy1DArraySubMenu();
  175. System.out.println("");
  176. }
  177.  
  178.  
  179.  
  180.  
  181. }
  182.  
  183.  
  184.  
  185.  
  186.  
  187. public static void main(String []args){
  188.  
  189. displayMainMenu();
  190.  
  191. }
  192.  
  193. }
  194.  
  195.  
  196. class Conversion {
  197.  
  198. double GramsToOunces(double weight){
  199. double answer = weight*0.03527396;
  200. return answer;
  201. }
  202.  
  203. double OunToGrams(double weight){
  204. double answer = weight*28.3495231;
  205. return answer;
  206.  
  207. }
  208.  
  209. double KgToStone(double weight) {
  210. double answer = weight*0.15747304;
  211. return answer;
  212. }
  213.  
  214.  
  215. double StoneToKg(double weight){
  216. double answer = weight*6.35029318;
  217. return answer;
  218. }
  219.  
  220.  
  221. double PoundsToGram(double weight){
  222. double answer = weight*453.59237;
  223. return answer;
  224. }
  225.  
  226.  
  227. double GramToPound(double weight) {
  228. double answer = weight*0.00220462;
  229. return answer;
  230. }
  231.  
  232.  
  233. }
  234.  
  235.  
  236.  
  237.  
  238. class My1DArray{
  239.  
  240. double getTotal (double arr[]){
  241. int length = arr.length;
  242. int size = length-1;
  243.  
  244. double total =0;
  245.  
  246. for(int i=0;i<=size;i++){
  247. total= total+ arr[i];
  248. }
  249. return total;
  250. }
  251.  
  252.  
  253.  
  254. double getAverage (double arr[]){
  255. int length = arr.length;
  256. int size = length-1;
  257.  
  258. double total =0;
  259. for(int i=0;i<=size;i++){
  260. total= total+ arr[i];
  261. }
  262.  
  263. double average = total/length;
  264.  
  265. return average;
  266. }
  267.  
  268.  
  269.  
  270.  
  271.  
  272.  
  273. }
Add Comment
Please, Sign In to add comment