Advertisement
Guest User

Untitled

a guest
Oct 10th, 2019
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.95 KB | None | 0 0
  1. package com.company;
  2. //import java.io.*;
  3. //import NewClass.java;
  4.  
  5.  
  6.  
  7.  
  8. public class Main {
  9.  
  10. public static class NewClass{
  11. int x = 10;
  12.  
  13. void printMessage(){
  14. System.out.println("This is NewClass");
  15. }
  16. }
  17.  
  18. static class DerivedClass extends NewClass {
  19. int y = 5;
  20.  
  21. @Override
  22. void printMessage() {
  23. System.out.println("This is DerivedClass");
  24. }
  25. }
  26.  
  27. public static class Item{
  28. private static int uniqueID = 1;
  29. private int itemID;
  30. private int param;
  31.  
  32. public Item(int param)
  33. {
  34. this.param = param;
  35. itemID = uniqueID;
  36. uniqueID++;
  37.  
  38. }
  39.  
  40. }
  41.  
  42. public static class exceptionThrown{
  43. static int divideByZero(int a, int b){
  44. int i = a/b;
  45. return i;
  46.  
  47. }
  48.  
  49. static int computeDivision(int a, int b){
  50. int res = 0;
  51. try{
  52. divideByZero(a, b);
  53. }
  54. catch(Exception ex){
  55. System.out.println("The exception was thrown!");
  56. }
  57. return res;
  58. }
  59. }
  60.  
  61.  
  62. public static void main(String[] args) {
  63. // write your code here
  64.  
  65. //System.out.println("Hello world!");
  66.  
  67. NewClass newObj = new NewClass();
  68. System.out.println("NewClass: " + newObj.x);
  69.  
  70. DerivedClass derivedObj = new DerivedClass();
  71. System.out.println("DerivedClass: " + derivedObj.x + " " + derivedObj.y);
  72.  
  73. newObj.printMessage();
  74. derivedObj.printMessage();
  75.  
  76. Item item = new Item(5);
  77. System.out.println(item.itemID);
  78.  
  79. exceptionThrown exception = new exceptionThrown();
  80.  
  81. int a = 1, b = 0;
  82. try{
  83. int i = exception.divideByZero(a, b);
  84. }
  85. catch(Exception ex){
  86. System.out.println(ex.getMessage());
  87. }
  88.  
  89.  
  90.  
  91. }
  92. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement