Home > Enterprise >  Am I missing methods in this Java UML Diagram?
Am I missing methods in this Java UML Diagram?

Time:11-05

I'm a CS student and learning Java. For my assignment, I've created the below UML diagram for the code below. This diagram should contain class name, variables and methods.

However, I am not sure if I created a correct UML diagram. I think class name and variables are correct, they are all public data members, but I'm not sure if the method is correct. It seems only method in my code is main method.

Can you advise if I'm correct or not please?

Code:

import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;
import java.io.File;

public class FileTest {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String fName = "Students.txt";  // variable to store the file name
       
        // prompt to user to enter number of names 
        System.out.print("Enter the number of names: ");
        int number = sc.nextInt();  // taking number of names
        // clear the input buffer
        sc.nextLine();
        
        ArrayList<String> names = new ArrayList<>(number); // arraylist to store the entered names 

        // take number of input names from the user
        for(int i=0;i<number;i  ){
            System.out.print("Enter name "   (i 1)   ": ");
            String name = sc.nextLine();
            names.add(name); // add name into arrayList
        }

        // sorting the names list
        Collections.sort(names);
        
        // store all names into the file Students.txt
        try {
            FileWriter fw = new FileWriter(fName); // open a file for writing names 
            
            for(String name:names){  // write names into files Student.txt
                fw.write(name   "\n");
            }
            
            fw.close(); // close file 
        } catch (IOException e) {
            e.printStackTrace();
        }

        //prompt to user to enter a name which you want to elininate from the file
        System.out.print("\nEnter a name that you want to eliminated from the file: ");
        String removeName = sc.nextLine();

        // make a temporary file to copy all the names except the names which you want to remove
        String tempFName = "StudentsTemp.txt";

        try {
            Scanner scan = new Scanner(new File(fName)); // open Student.txt file for reading names

            // open temp file to write all the data except eliminated name
            FileWriter fw = new FileWriter(tempFName);

            // read all data
            while(scan.hasNextLine()){
                String name = scan.nextLine(); // read name
                //check the readed name with the user entered eliminated name
                if(name.compareTo(removeName)!=0){
                    fw.write(name   "\n");
                }
            }
            // close file
            fw.close();
            //delete the old file
            File old = new File(fName);
            old.delete();

            
            File newFile = new File(tempFName); //rename the temporary file to the old file name
            newFile.renameTo(old);

        } catch (Exception e) {
            e.printStackTrace();
        }
        
    }

    
}

UML Diagram: uml diagram

I think only method is main method, but what about rename, delete, compareto, etc.? aren't these methods should be included to uml diagram as well?

CodePudding user response:

This is a UML class diagram "cheat sheet"

The box

  • Class diagram has three boxes, whereas interfaces only have two
  • The top section of the diagram is reserved for the class or interface name
  • The bottom section of the diagram is reserved for the class or interface methods
  • The middle section of a class diagram is reserved for its (global) variables

Visibility (access) modifiers

  • public access (it is acceptable to omit this symbol)
  • - private access
  • # protected access
  • ~ default access. I think this was not originally part of class diagrams, but was added later to accommodate Java's package-private modifier

Other notes

  • Static members (class or variables) are denoted by underlining the name
  • Constant fields are denoted by CAPITALIZING the name
  • A method's parameter list is defined to the right of the method name inside parentheses. It shall include type and variable name separated by colon. For example: myMethod(name : String, value : int)
  • A method return type is defined to the right of the method name separated by a colon. For example: myMethod(String, int) : void
  • Constructors (obviously) won't have return types

A (simple) example

This is a simpe example of a class diagram putting in practice all elements in this cheat sheet.

A simple example of a class diagram

CodePudding user response:

To answer your question per title: no. But it's utterly wrong. There is no class in the sense of a class. This is what Java came up with to place that operation main. And that's the only operation in this pseudo-class. Nothing else. No attributes. No other operation.

(will put a diagram picture here later)

  • Related