Why should I import "java.util.*" in top of my code? - java.util.scanner

my question is that i want to use Scanner object or Arrays object for example for usage of Arrays.copyof, but before importing java.util.* or java.util.Scanner and java.util.Arrays there is now object of them to use !
why this happens to me??enter image description here

It is a little unclear what you want to achieve. Are you asking why you should do imports in Java?
import statements allow you to refer to classes which are declared in other packages without referring to their full package name. This is standard java practice. Inside your main(String args[] you can use java.util.Scanner myobject = new .. if you want, but that is too cumbersome and often you have multiple classes in a package that you want to use. So adding a simple import java.util.* is considered better.
Another tip is to use one among the large number of available IDEs (eclipse, sublime, IntelliJ) which will add the imports automatically for you.

Related

How to distribute python classes into different source files without creating many modules?

I have a few related classes sitting in a package like this:
\cool
\__init__.py
class CoolObj: ...
class CoolStr(CoolObj): ...
class CoolName(CoolStr): ...
class CoolSound(CoolObj): ...
And I use these classes like this:
import cool
cn = cool.CoolName()
...
This __init__.py file is getting too long. It would be nice if I could put each class in each of their own file like this:
\cool
\__init__.py
\cool_obj.py
class CoolObj: ...
\cool_str.py
class CoolStr(CoolStr): ...
\cool_name.py
class CoolName(CoolStr): ...
\cool_sound.py
class CoolSound(CoolObj): ...
However, this way, these files becomes modules and I'd have to use them as below, which is a little verbose.
from cool import cool_obj, cool_str, cool_name, cool_sound
cn = cool_name.CoolName()
Is there anyway that can put source code in separate file while still be able to refer to them with concise syntax like I do now?
Better even, is there anything in python like partial class in C# that allows you to spread implementation of the same class in different files?
In your __init__.py you can do the imports to shorten how you use them elsewhere. So a line in __init__.py could be
from cool_name import CoolName
And later on you could do
from cool import CoolName
You may also want to look at __all__ as another way of solving this problem. You could import all sorts of stuff in the cool.__init__.py module and as long as you include an __all__ you can explicitly specify what objects will be exported.
Regarding partial classes, I am almost sure Python does not support this kind of functionality through regular class definitions. You could hack it together by injecting different functions into a class at runtime, but that would certainly be messy. This answer takes a deeper look at that path.

OOP : Name Error while trying to make a object instance

I have a class called FIT, saved in a file called manage.
in my main file, the first lines look like this
import manage
FITobj= FIT()
I thought when I did this it would call the class so I would be able to use functions like get_balance() like FITobj.get_balance(). but instead, when I try to run my program I get name error name FIT is not defined. I'm fairly new to object-oriented programming, can someone help?
You need to indicate where the FIT class is defined. Either change it to:
import manage
FITobj = manage.FIT()
Or
from manage import FIT
FITobj = FIT()
The first is slightly more typing, but it helps keep your code more readable because as you import more and more, it can get very difficult to keep track of where everything is coming from.
Adding to #mypetlion's answer, to avoid even more typing (but make your program even less readable), you can do this:
from manage import *
FITobj=FIT()
And then use anything else from manage as well as just FIT.

Creating libraries that can be imported and used in Groovy

Currently, I am working on a project to transpile from my company's in house scripting language, which is Object Orientated and takes quite a few features from other languages, into Groovy, which has many similar features.
To keep code as close to original as possible, I am trying to leave certain function names and parameters the same. To cater for this, I would like to write a set of libraries that can be imported.
For example, say I have an inbuilt method in the original scripting language,
I would like to be able to write the definition for this method in a groovy file, that can then be imported when needed, and the method may be called.
Tools.groovy
// filename: Tools.groovy
public String foo(String bar) {
return bar;
}
and in another file
Main.groovy
// filename: Main.groovy
import Tools;
String bat = foo("bar")
I know you can can compile class files into jars and put them into the class path, but a lot of the methods I will need to implement will either require meta programming or won't be associated with an object.
Sorry if it's either a bad question or not clear enough. I'm not sure whether its even possible.
Cheers
I believe you should be able to create libraries and reuse them when needed.
All you need to do is create class and add the static methods if you do not have to create instances, non static methods otherwise. Then it looks like you already aware how to proceed later.
For instance, you can create utilities classes for String, List, etc based on your description.
By the way, even if you do not create libraries, it is even possible to write one lines in groovy achieve what you may needed most of the cases.

How to do custom python imports?

Is there a way to have custom behaviour for import statements in Python? How? E.g.:
import "https://github.com/kennethreitz/requests"
import requests#2.11.1
import requests#7322a09379565bbeba9bb40000b41eab8856352e
Alternatively, in case this isn't possible... Can this be achieved in a standard way with function calls? How? E.g.:
import gitloader
repo = gitloader.repo("https://github.com/kennethreitz/requests")
requests = repo.from_commit("7322a09379565bbeba9bb40000b41eab8856352e")
There are two reasons for why I would like to do this. The first reason is convenience (Golang style imports). The second reason is that I need cryptographic verification of plugin modules for a project. I'm using Python 3.x
What you're essentially asking is customization of the import steps. This was made possible with PEP 302 which brought about certain hooks for for customization.
That PEP is currently not the source from which you should learn how importing works; rather, look at the documentation for the import statement for reference. There it states:
Python includes a number of default finders and importers. The first one knows how to locate built-in modules, and the second knows how to locate frozen modules. A third default finder searches an import path for modules. The import path is a list of locations that may name file system paths or zip files. It can also be extended to search for any locatable resource, such as those identified by URLs.
The import machinery is extensible, so new finders can be added to extend the range and scope of module searching.
In short, you'll need to define the appropriate finder (to find the modules you're looking for) and an appropriate loader for loading these.

Reserved Class Names in Groovy

If I have a class named Character.groovy (with no explicit constructors) and try to instantiate it, I get a message that says:
groovy.lang.GroovyRuntimeException: Could not find matching constructor for: java.lang.Character()
But if I change the class name to Characterr.groovy, then I am able to instantiate an object and use it as expected. So are there reserved words I can't use in Groovy classes? If so, why is Character one of them?
It's not a reserved class name, but there already is a class with that name (java.lang.Character) imported as the package java.lang gets imported automatically in java.
This can happen all the time, especially if you are a java developer and not used to get e.g. java.io package etc. autoimported for you by groovy (e.g. File) (see also What packages does 1) Java and 2) Groovy automatically import?)
There are three ways around it:
the java way: address your class with the full name, that is package and class name. e.g. org.myrpg.Character.
the groovy way: import the class with a new name. e.g. import org.myrgp.Character as RPGChar and use then RPGChar instead.
the zen way: more often than not, it is not worth the hastle and easier to just rename your class. if you tripped over this once, then the chance very high you will trip over this again and only things like #CompileStatic or an IDE may make notice you this at compile time or while writing it.
http://groovy.codehaus.org/Reserved+Words
Those are the reserved keywords
http://docs.oracle.com/javase/8/docs/api/java/lang/Character.html
Character I believe is a object wrapper class in java which is why you can't use it. You can't use any name of java classes that are autoincluded in java

Resources